Monday, December 27, 2010

Get sharepoint workflow name programmatically

Recently we came across a scenario where i need to get the unique Name which we were given while attaching my custom workflow to a list in the association page like below





While finding through various variables exposed by workflowProperties we found that it has a workflowProperties.TemplateName which stores this unique name.

Nothing big i said :) , but hopes that it may help someone while trying to access a workflow programmatically.

Friday, November 26, 2010

Share Point User Information List : Few Facts

Recently we were working on User Information List in WSS and observed few facts like

1) We can't attach an Event Handler on this User Information List like any other list to capture user profile updation.
2) We can't create a custom search scope directly against User Information List.
3)User Info List has 2 Views namely a) Default View b) List View . These views only appear for all sharepoint groups in this site collection.
4) If we create a new view to display more custom fields the same view available for all the sharepoint groups.(i.e. If we want to change or create a new view for all sharepoint groups change it in this list as it is the root for all group members data)

Saturday, October 2, 2010

Find All The Sharepoint Groups For Which a User Belongs

As the size of share point application grows on with more and more sites and sub sites number of share point groups also increases proportionately. In most of these cases it becomes extremely difficult for site administrators to identify which all groups a particular user belongs. Share point as such doesn’t provide an interface or a place in-built where we can list all the groups a particular user belongs. Many of the customers with which we are working came up asking for a solution which solves this.

Share point’s SPUser object has a property called SPUser.Groups which returns a SPGroupCollection which gives the collection of groups of which the user is a member.

Monday, September 6, 2010

Setting value of Read Only fields programmatically in Sharepoint

When we are trying to update values of list default columns like created by, modified by programmatically it throws an error saying “The field you are trying to update may be read only” .However we can set the values of these Read Only columns by using their Internal Name (For more info on Internal,External names of a field refer
here)

Let us assume that I need to update 2 such column namely “Created By” and “Modified By” then code can be like

using (SPSite site = new SPSite(SPContext.Current.Site.ID))
{
using (SPWeb web = site.OpenWeb())
{
site.AllowUnsafeUpdates = true;
web.AllowUnsafeUpdates = true;
// Access the List
SPList list = web.Lists["Your_List_Name"];
// Access the item for which you want to update values
SPListItem listItem = list.Items[6];// choose the index corresponding to item
// Set the value for Created By field
listItem["Author"] = SPContext.Current.Web.CurrentUser;
// Set the value for Modified By field
listItem["Editor"] = SPContext.Current.Web.CurrentUser;
listItem.Update();
web.Update();
}
}

Like this we can programmatically set value of any field through its internal name.

Wednesday, June 23, 2010

Internal Name of a Sharepoint List Column

When we create a new column for list, SharePoint creates two names for it.
1. External name to display in views, forms
2. Internal name

By default, the name we specify while creating the column is used as display name as well as internal name. However we can change the display name as many times as we want, but can never change the internal name once column is created.

This Internal Name is only used in any of the query we are performing on the column(For example while writing CAML query or while using the content by query web part).

In most of the scenario’s we may need to change the display name of column as customer demands and at the same time we should make sure that the code we wrote to fetch values from these columns should not be altered every time we change the display name of column.To handle this as a best practice always use the this internal column name in the code-behind as well.

As an example assume that, we have a list names SPList with 2 columns (Title,ItemID) as below




Here an ItemID column is created just be specifying a name in Column Name option. So it has External Name = Internal Name = ItemID)
To find this intenal name of the column ItemID, Go to the list settings and click on column. This Internal name will be in the URL of the browser (field=) for example
http://localhost:2000/_layouts/FldEdit.aspx?List=%7B17ED484C%2DF98A%2D4489%2DA687%2DE24F12AF1C31%7D&Field=ItemID

so now in the code-behind access this column value like

using (SPSite site = new SPSite("http://localhost:2000/"))
{
using (SPWeb web = site.OpenWeb())
{
SPList list = web.Lists["SPList"];

foreach (SPListItem item in list.Items)
{
Console.WriteLine(item["ItemID"].ToString());
}
Console.Read();
}
}


However at the same time if required change the display name of column like








We need not have to change the anything in code as we used internal name there. This is the advantage we have by share point’s behavior of creating two names(External Name and Internal Name) for every column we create.

Thursday, May 20, 2010

Programmatically get directory path to 12 hive in share point

In Our project recently We came across a requirement that while deployment of solution package (WSP) in the production server needed to run a feature activation code which refers some data from some Static files (Say .xml). As a good approach we had put this xml file in the 12-hive file system path of share point say ~features\MyCustomFeature\MyCustomXML.xml and in our feature activation code we referred this file path using SPUtility.GetGenericSetup method like

string myCustomFilePath = SPUtility.GetGenericSetup (“template\\features\\MyCustomFeature\\MyCustomXML.xml”);

Basically this method returns the file system path for the 12-Hive, or any of the folders beneath it. This is typically C:\Program Files\Common Files\Microsoft Shared\web server extensions\12.

One of the greatest advantages of this method is that in production servers even though usually share point is installed under c drive there may be scenarios where for better performance it could have been installed in any drive. So our code need not have to be hard coded for sharepoint installation path of server and works in all scenarios.

Friday, April 30, 2010

Impersonation technique using the SPUserToken in share point

Recently I had a requirement where I have share point list say with 10 items and item level permissions is set for each of the item.Assume that a user user1 has permission only for first 4 items,user2 only for next 3 items and user3 only for last 3 items.

Then I need to query this share point list with CAML query to fetch the items based on some criteria based on logged-in user. However as each user have only read permission and that too on few items the query using object module was giving access denied error.

So I had no other option other than to run the code with elevated privileges. I used impersonation technique using SPUserToken and SPSite class.
To impersonate the system I used the SystemAccount.UserToken property of the current SPSite context like
SPSite sharepointSite = new SPSite(SPContext.Current.Site.ID,SPContext.Current.Site.SystemAccount.UserToken);
then SPWeb object as

SPWeb sharepointWeb = sharepointSite.OpenWeb();
finally my sharepoint list as
SPList sharepointList = sharepointWeb.Lists["My List Name"];
and everything worked.

Wednesday, January 27, 2010

Creating Custom Site Defination with custom master page in share point 2007

In this article i am showing step by step to create a custom site defination with costom master page for MOSS 2007.
1) Copy any of the existing site tepmplate and paste it in SiteTempalte folder of the server computer.
Copy th team site tempale from folder C:\Program Files\Common Files\Microsoft shared\web server extensions\12\TEMPLATE\SiteTemplates and paste it at the same folder level. Rename it to MOSSSITE. So its full path will be now C:\Program Files\Common Files\Microsoft shared\web server extensions\12\TEMPLATE\SiteTemplates\MOSSSITE.
2) Now copy the custom master page which you have mosssite.master to the newly created site definations root folder. i.e. to C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\TEMPLATE\SiteTemplates\MOSSSITE
3) In the C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\TEMPLATE\SiteTemplates\MOSSSITE folder for both default.aspx and defaultdws.aspx file change the master page to a new master page i.e custom.master like below


Note : This not the master page.We are going to configure Share point to do a string replacement of this custom.master with mosssite.master in later steps.
4) Modify the onet.xml file
This is the file that tells share point how to create the site defination.We are going to do specify all the required modification here.
a)Open the onet.xml file from the path C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\TEMPLATE\SiteTemplates\MOSSSITE\xml and remove the unwanted code-snippets
i) Identify the Configurations XML node and remove the entire nodes corresponding to
ID = 1 - Representing Blank
ID = 2 - Representing DWS
ii) Scrowl down to Modules xml node and remove 2 modules namely DefaultBlank and DWS
b) Now add the following code snippets
i) Identify the configuration element with id=0 and add the following attributes


ii) Locate the module child element within the configuration element and add the following Module element




iii) Locate the module child element ouside the configuration element and add the following module and file elements


Save all these changes of onet.xml file.
5) So far, we have successfully created a site definition. All we have to do now is to register this definition to the site definition list. To do this, create "WEBTEMPMOSSSITE.XML" file in "C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\TEMPLATE\1033\XML" folder.

6) Write the following code in this file and Save the file.





7)To make sure that our new Site Definition has all the right pieces and parts, navigate to the \12\Template\Features\BaseSiteStapling folder and open BaseSiteStapling.xml.




It should look something like this




Since our new Site Definition needs to behave exactly as the STS definition does,we must create a <> for each Feature that STS is associated with, like so:

Simply go through BaseSiteStapling.xml and each time you see an ID associated with STS#0 add an entry for MOSSSITE like below



8) Do the same thing in these 2 files

i)premiumsitestapling.xml located in C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\12\TEMPLATE\FEATURES\PremiumSiteStapling folder

ii)featurestapling.xml located in C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\12\TEMPLATE\FEATURES\StapledWorkflows folder

9) Save all the stuffs and do an iisreset
10)Now go to any site and select create site option . Under MOSS tab you will find the template
like below










Thats it :) .