Create Custom Claims Provider In SharePoint 2013 Using C# Server-Side Object Model

In this article, you will learn how to create Custom Claims Provider in SharePoint 2013 using C# Server-Side Object Model.

Introduction

Custom Claims Provider issues and packages claims into security tokens which can be further used to give permission to the items in a customized way. Claims Augmentation enables an application to augment additional claims into the user token. Claims can be displayed in the people picker control through claims picking. In this article, I will explain how to create Custom Claims Provider in SharePoint 2013 using C# Server-Side Object Model.

Pre-Requisites

  1. Open Visual Studio.

  2. Open New Project dialog box. Expand Office/SharePoint node and then choose SharePoint Solutions.



  3. Choose the SharePoint 2013 – Empty Project template. Name the project as ClaimProviderProject.



  4. Choose the Deploy as a farm solution option button and click Finish.



  5. To create a Custom Claims Provider class file, right click ClaimProviderProject project -> Add -> New Item.



  6. Add the class file and name it as CustomClaimsProvider.cs.

     

Create Custom Claims Provider

  1. Inherit SPClaimProvider to the new class created by adding Microsoft.SharePoint.Administration.Claims reference.



  2. Add the fields for Claims Provider as Display Name, Internal Name, and People picker display name.
    1. #region FIELDS    
    2. public    
    3. const string ClaimsProviderDisplayName = "Custom Claims Provider";    
    4. public    
    5. const string ClaimsProviderInternalName = "CustomClaimsProvider";    
    6. private    
    7. const string PickerEntitySchemaDisplayName = "Custom Claims";    
    8. #endregion FIELDS    
  3. Add the properties for Display Name and Internal Name.
    1. #region PROPERTIES    
    2. public virtual string ProviderDisplayName {    
    3.     get {    
    4.         return ClaimsProviderDisplayName;    
    5.     }    
    6. }    
    7. public virtual string ProviderInternalName {    
    8.     get {    
    9.         return ClaimsProviderInternalName;    
    10.     }    
    11. }    
    12. #endregion PROPERTIES   
  4. Override the SPClaimProvider properties, as shown below.
    1. #region OVERRIDDENPROPERTIES    
    2. public override string Name {    
    3.     get {    
    4.         return this.ProviderInternalName;    
    5.     }    
    6. }    
    7. public override bool SupportsEntityInformation {    
    8.     get {    
    9.         return true;    
    10.     }    
    11. }    
    12. public override bool SupportsHierarchy {    
    13.     get {    
    14.         return true;    
    15.     }    
    16. }    
    17. public override bool SupportsResolve {    
    18.     get {    
    19.         return true;    
    20.     }    
    21. }    
    22. public override bool SupportsSearch {    
    23.     get {    
    24.         return true;    
    25.     }    
    26. }    
    27. #endregion OVERRIDDENPROPERTIES    
  5. Create the constructor for newly created class.
    1. #region CONSTRUCTOR    
    2. public CustomClaimsProvider(string displayName): base(displayName) {}    
    3. #endregion CONSTRUCTOR    
  6. Override the SPClaimProvider methods as the following.

  7. Override the FillClaimTypes method to add your custom claims type.
    1. protected override void FillClaimTypes(List < string > claimTypes)     
    2. {    
    3.     // claimTypes.Add("Our Custom Claim Type");      
    4. }    
  8. Override the FillClaimValueTypes method to add your custom value type.
    1. protected override void FillClaimValueTypes(List < string > claimValueTypes)     
    2. {    
    3.     //claimValueTypes.Add("Our Custom Value Type");      
    4. }    
  9. Override the FillClaimsForEntity method to add your custom entities.
    1. protected override void FillClaimsForEntity(Uri context, SPClaim entity, List < SPClaim > claims)    
    2. {    
    3.     // claims.Add("Our Custom Entity");      
    4. }   
  10. Override the FillEntityTypes method to add your custom entities type.
    1. protected override void FillEntityTypes(List < string > entityTypes)    
    2. {    
    3.     entityTypes.Add(SPClaimEntityTypes.FormsRole);    
    4. }    
  11. Override the FillResolve method to add your custom resolve claim.
    1. protected override void FillResolve(Uri context, string[] entityTypes, SPClaim resolveInput, List < Microsoft.SharePoint.WebControls.PickerEntity > resolved)    
    2. {    
    3.     //PickerEntity pe = GetPickerEntity(resolvedClaim.ClaimType, resolvedClaim.ClaimValue, resolvedClaim.ClaimValueType);      
    4.     //Add it to the return list of picker entries.      
    5.     //resolved.Add(pe);      
    6. }    
    7. protected override void FillResolve(Uri context, string[] entityTypes, string resolveInput, List < Microsoft.SharePoint.WebControls.PickerEntity > resolved) {    
    8.     //PickerEntity pe = GetPickerEntity(resolvedClaim.ClaimType, resolvedClaim.ClaimValue, resolvedClaim.ClaimValueType);      
    9.     //Add it to the return list of picker entries.      
    10.     //resolved.Add(pe);      
    11. }    
  12. Override the FillSchema method to add your custom schemas.
    1. protected override void FillSchema(Microsoft.SharePoint.WebControls.SPProviderSchema schema)    
    2. {    
    3.     //add the schema element we need at a minimum in our picker node      
    4.     schema.AddSchemaElement(new SPSchemaElement(PeopleEditorEntityDataKeys.DisplayName, PickerEntitySchemaDisplayName, SPSchemaElementType.Both));    
    5. }    
    6.    
  13. Override the FillSearch method to add your custom search claim. 
    1. protected override void FillSearch(Uri context, string[] entityTypes, string searchPattern, string hierarchyNodeID, int maxCount, Microsoft.SharePoint.WebControls.SPProviderHierarchyTree searchTree)     
    2. {    
    3.     //Nodes where we will stick our matches.      
    4.     // Microsoft.SharePoint.WebControls.SPProviderHierarchyNode matchNode = null;      
    5.     //Add the match to our node.      
    6.     //matchNode.AddEntity(pe);      
    7. }    
  14. Add the SharePoint Feature to this project and set it as Farm level.



  15. Deploy this solution to the site.

  16. Activate the custom Claims Provider using Windows PowerShell script.

    • Set the IsEnabled to True for the Claim Provider.
    • Add provider association to the Web Application.
   17. Check if the custom Claims Provider is populated in the assigning permission to an item.

Summary

Thus, you have learned how to create Custom Claims Provider in SharePoint 2013 using C# Server-Side Object Model.