SharePoint : Get User’s login name from claims encoded value

There are sometimes scenarios where we have to get user’s login name and we have only claims encoded value (like this i:0#.w|domain\username or i:0#.f|membershipprovider|abc.xyz@comp.com). In SharePoint 2013 / 2010, there is SPClaimProviderManager class available which can decode the claims and provide user’s login name.

I have created a method, which accepts claims encoded value as parameter and returns user’s login name (only if claims are resolved properly).

private string GetUsernameFromClaim(string claimsEncodedUsername)
{
 try
 {
 SPClaimProviderManager mgr = SPClaimProviderManager.Local;
 if (mgr != null)
 {
 if (SPClaimProviderManager.IsEncodedClaim(claimsEncodedUsername))
 {
 // return the normal domain\username without any claims identification data
 return mgr.DecodeClaim(claimsEncodedUsername).Value;
 }
 }
 }
 catch (Exception ex)
 {
 return claimsEncodedUsername; // Or just return the original username.
 }

 // Return the original username value if it couldn't be resolved as a claims username
 return claimsEncodedUsername;
}

Note : Add namespace “Microsoft.SharePoint.Administration.Claims” to use above class.

SharePoint SPFile : MoveTo and CopyTo method

In SharePoint, using object model, files can moved from one folder to another folder or one library to another library within the same site collection, For this, SPFile object has two methods :

  • MoveTo(string)
  • CopyTo(string)

SPFile.MoveTo(string destinationURL)

This method moves the original file with all metadata and version history from one location to another location by providing the destination url as a parameter.

SPWeb spWeb = SPContext.Current.Site.RootWeb;
SPFileCollection allFiles = spWeb.GetFolder("Source_Folder").Files;
for (int intIndex=allFiles.Count-1; intIndex>-1; intIndex--)
{
   allFiles[intIndex].MoveTo("Destination_Folder/" + allFiles[intIndex].Name);
}

 

SPFile.CopyTo(string destinationURL)

This method copies the original file with all metadata from one location to another location by providing the destination URL as a parameter.

SPWeb spWeb = SPContext.Current.Site.RootWeb;
SPFileCollection allFiles = spWeb.GetFolder("Source_Folder").Files;
for (int intIndex=allFiles.Count-1; intIndex>-1; intIndex--)
{
   allFiles[intIndex].CopyTo("Destination_Folder/" + allFiles[intIndex].Name);
}

 

Note : Both methods have one more overloaded method which accepts Boolean as parameter:

  • SPFile.MoveTo(string, boolean)
  • SPFile.CopyTo(string, boolean)

As per msdn, if this parameter is true then it will overwrite the existing file with same name in destination URL. But these methods work differently, if there is any existing file in destination URL then it is first deleted and moved to Recycle Bin and after that original file is moved or copied to destination URL.

Feature Stapling in SharePoint

I want a custom list to be created by default whenever a site is created using the Team Site template. The best way to do this is to create a Feature Stapling.

Feature Stapling can be created for the site definition that is already in use. Feature Stapling has two features.

  • Stapler feature: staples another feature to the site definition
  • Staplee feature: that which will be stapled

Steps involved:-

The following is the procedure steps to to implement Feature Stapling in SharePoint:

  • Create Empty SharePoint Project
  • Create a feature
  • Create the feature stapler
  • Create FeatureAssociation.xml
  • Deploy the solution
  • Check whether the feature stapler is activated in a web application
  • Testing

Create Empty SharePoint Project

  • Open Visual Studio 2010 by going clicking “Start” then select “All Programs” | “Microsoft Visual Studio 2010” then right-click on Microsoft Visual Studio 2010 and click on “Run as administrator”.
  • Go to the File tab, click on “New” and then click on “Project”.
  • In the New Project dialog box, expand the Visual C# node, and then select the SharePoint 2010 node.
  • In the Templates pane, select “Empty SharePoint Project”.
  • Enter the Name as FeatureStapling and then click “OK”.

featureStaplingPrj

  • Enter the local site URL for debugging, select “Deploy as a farm solution” as in the following and then click on “Finish”.

Create a feature

In this section you will see how to create a new feature and feature receiver to create a custom list in the site.

  • In the Solution Explorer, right-click on the Feature folder and then click on “Add Feature”.

featureStaplingPrj2

  • Rename the feature to “CreateListFeature”.
  • Double click and enter the title & description ofthe feature .
  • Select the scope as web from the drop down list.
  • Right-click on CreateListFeature and then click on the Add Event receiver.

featureStaplingPrj3

  • Double-click on CreateListFeature.EventReceiver.cs and replace the code with the following:
protected override void FeatureDeactivatingInternal(SPFeatureReceiverProperties properties)
{
     SPWeb web = (SPWeb)properties.Feature.Parent;
     string listUrl = SPUrlUtility.CombineUrl(web.ServerRelativeUrl, "Lists/CertificateInformationTool")
     SPFolder folder = web.GetFolder(listUrl);
     if (folder.Exists)
     {
         SPList list = web.Lists[folder.ParentListId];

         list.Recycle();
     }
}
  • The Code above creates a list on the feature activation .

Create feature stapler

In this section you will see how to create a feature stapler that is used to staple the CreateListFeature to the site definition.

  • In the Solution Explorer, right-click on the Feature folder and then click on “Add Feature”.

featureStaplingPrj4

  • Rename the feature to “FeatureStapler”.

featureStaplingPrj5

  • Double-click on the feature and enter the Title and Description for the feature.

featureStaplingPrj6

  • Select the scope as WebApplication from the drop down list.

Create FeatureAssociation.xml

Now we need to create the XML file to associate the feature to the site definition.

  • In the Solution Explorer, right-click on the project, click on “Add” and then click on “New Item”.
  • Select the “Empty Element” template, enter the Name and then click on “Add”.

featureStaplingPrj7

  • Double-click on Elements.xml and replace with the ListFeature feature Id.

                 Template Name : To which site template the feature should be associated (the                                                            format should be <site template name><configuration                                                                        number>).

  • Ensure this file is added to the FeatureStapler feature (double-click on the FeatureStapler feature and see whether in the “Items in the feature” window
    the Elements.xml file is available).

featureStaplingPrj8

Deploy the solution

  • Right-click on the solution and then click on “Deploy Solution”.
  • Check whether the feature stapler is activated in the web application.

Testing

Now whenever we go to any of the site collections and look for the particular feature, it should be activated .
Thus, we can have this list on any site collection in a particular web application when we have this feature activated and vice-versa.

featureStaplingPrj9

References

https://msdn.microsoft.com/en-us/library/vs/alm/bb861862(v=office.12).aspx

SharePoint file download programmatically

In SharePoint, the file in document library can be downloaded using c# code . In the code below, provide the “ID” of the item and the document library name to download the file. User can download all types of file and will be prompted to save the file.

Continue reading SharePoint file download programmatically