Tuesday, December 20, 2011

How to Customize SharePoint List Content Display using Content Query Web Part

Here I'm going to describe how we can use Content Query Web Part to Style SharePoint List. Thus Assume we are having a  SharePoint List Called Projects with Following Fields.

image

As you already knew default look and feel would be like below.

image

Now I'm going to render this list in to format something like below.

image

Step One

First you need to define Some names for these place holders (Yeh., I know this do not make any sense to you. But don’t worry just do the step). Thus I'm going to define these columns as follows.

  • Title –> MyTitle
  • Logo –> MyLogo
  • Project Summary –> MySummary
  • Project Description –> MyDesc

Step Two

Now you want to define a XSL Style Sheet using these names. Therefore I'm going to use <table> to layout the list item as below.

<xsl:template name="MyStyle" match="Row[@Style='MyStyle']" mode="itemstyle">
 
<table border='1'>
    <tr>
        <td colspan="2"><h3><xsl:value-of select="@MyTitle" /></h3></td>
    </tr>
    <tr>
        <td>
            <img>
            <xsl:attribute name="src">
                <xsl:value-of select="@MyLogo" />
            </xsl:attribute>
            </img>
        </td>
        <td>
            <xsl:value-of disable-output-escaping="yes" select="@MySummary" />
         </td>
    </tr>
    <tr>
        <td colspan="2">
            <xsl:value-of disable-output-escaping="yes" select="@MyDesc" />
        </td>
    </tr>
    </table>
 
</xsl:template>

here i used XSLT to position the content inside my html tags. There are few attributes which are mandatory in this section. 



  • Name and Match should have equal names.

    • name =’X’ match=Row[@style=’X’]

  • mode = ‘itemstyle

now you have created XSLT which can given as a list style in the SharePoint.


Step Three


here we are going to put XSLT to the SharePoint.


First you need to open the SharePoint designer with the particular site. Then navigate to All Files and Style Library.


image  


Here you can see many Folders inside. Now you have to go to XSL Style Sheet Folder.


image


Now you can see XSL files which are used by SharePoint to render it’s content. Here we are using ItemStyle.xsl for enable custom rendering.


image


Thus open this xsl file and put your xsl (when we created in step two) in the bottom of the xsl as follows.


 image



Now difficult things are over.


Step Four



  • Add a Content Query Web Part

image



  • then go to edit web part and  and browse your list. ( here our list is Our Projects )



image



  • Then go to presentation section. and go little bit down. You can see style drop down.
  • Wow ..  Now you can see your style.

image



  • Ok the then select your custom xsl ( here MySample).
  • When you select you can see something like below.

image



  • Now you need to put actual list column names to these text boxes as follows.(Title column should use with the scope)

image




  • Then Click ok to Save. Now Every thing ok. You can see your new list rendering now.

image


Happy SharePoint :)

Wednesday, December 14, 2011

How to use SharePoint Feature to Activate and Deactivate Custom SharePoint Master page

Below example only set the Master page Url but not the custom master page URL. thus Layout pages will be same after you deploy the master page.  You can also change the Custom master page URL also.

Feature Activation

public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
SPWeb web = properties.Feature.Parent as SPWeb;

string urlMaster;
string urlCustom;

if (@"/".Equals(web.ServerRelativeUrl))
{
urlMaster = @"/_catalogs/masterpage/BLLICT.master";
urlCustom = @"/_catalogs/masterpage/v4.master";
}
else {
urlMaster = string.Concat(web.ServerRelativeUrl , @"/_catalogs/masterpage/BLLICT.master");
urlCustom = string.Concat(web.ServerRelativeUrl , @"/_catalogs/masterpage/v4.master");
}

web.MasterUrl = urlMaster;
web.CustomMasterUrl = urlCustom;

web.Update();

}

Feature Deactivation (Reverse to Original Master)

public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
{
SPWeb web = properties.Feature.Parent as SPWeb;
string urlCustom;

if (@"/".Equals(web.ServerRelativeUrl))
{
urlCustom = @"/_catalogs/masterpage/v4.master";
}
else
{
urlCustom = string.Concat(web.ServerRelativeUrl, @"/_catalogs/masterpage/v4.master");
}

web.MasterUrl = urlCustom;
web.CustomMasterUrl = urlCustom;

web.Update();
}

Wednesday, December 7, 2011

How to impersonate SharePoint Programmatically

There are many occurrences that we need to impersonate our SharePoint requests to server. Otherwise SharePoint will use system account or context credentials to perform the task.

In my case i want to programmatically approve a workflow task. but then i noticed it is showing modifiedby as   system account so i used following code to change the user credentials to intended user.

SPUserToken ApprovingUser = web.GetUserToken(“domain\username”);

using (SPSite site = new SPSite(“Site Url”, ApprovingUser))
{
using (SPWeb wb = site.OpenWeb())
{
/* Do Anything You want */
}
}

SPWorkflowTask.AlterTask throws “Specified method is not supported”

This error occurred to me when i was trying to programmatically approve a task in the SharePoint 2010.  

Later on I found that the problem is the Task List do not have the content Type WorkFlow Task inherited. After i add that content type it worked for me.

Thursday, December 1, 2011

How to Use & Character in SharePoint List Instance’s Field value

There are some special characters which we can use in the XML. thus XML parser will return something like Entity X not defined like errors. this problem comes using ok special characters such as &, > and etc.

Following shoes hoe you can use XML special characters inside the Field values in XML or SharePoint list instance creation.

   <Data>
<
Rows>
<Row>
<Field Name="Value" ><![CDATA[ & ]]></Field>
</Row>
</
Rows>
</
Data>
you can use any special character inside the  CDATA tag.