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 a Custom Claims Provider in SharePoint 2013 using the C# Server-Side Object Model.

Pre-Requisites

Open Visual Studio.

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

 Visual Studio

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

Project template

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

Deploy

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

 New Item

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

 Class file

Create Custom claims provider

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

 Claims Provider

Add the fields for Claims Provider as Display Name, Internal Name, and People picker display name.

#region FIELDS
public const string ClaimsProviderDisplayName = "Custom Claims Provider";
public const string ClaimsProviderInternalName = "CustomClaimsProvider";
private const string PickerEntitySchemaDisplayName = "Custom Claims";
#endregion FIELDS

Add the properties for Display Name and Internal Name.

#region PROPERTIES
public virtual string ProviderDisplayName
{
    get
    {
        return ClaimsProviderDisplayName;
    }
}
public virtual string ProviderInternalName
{
    get
    {
        return ClaimsProviderInternalName;
    }
}
#endregion PROPERTIES

Override the SPClaimProvider properties, as shown below.

#region OVERRIDDENPROPERTIES
public override string Name
{
    get
    {
        return this.ProviderInternalName;
    }
}
public override bool SupportsEntityInformation
{
    get
    {
        return true;
    }
}
public override bool SupportsHierarchy
{
    get
    {
        return true;
    }
}
public override bool SupportsResolve
{
    get
    {
        return true;
    }
}
public override bool SupportsSearch
{
    get
    {
        return true;
    }
}
#endregion OVERRIDDENPROPERTIES

Create the constructor for the newly created class.

#region CONSTRUCTOR
public CustomClaimsProvider(string displayName) : base(displayName) {}
#endregion CONSTRUCTOR

Override the SPClaimProvider methods as the following.

Override the FillClaimTypes method to add your custom claims type.

protected override void FillClaimTypes(List<string> claimTypes)
{
    // claimTypes.Add("Our Custom Claim Type");
}

Override the FillClaimValueTypes method to add your custom value type.

protected override void FillClaimValueTypes(List<string> claimValueTypes)
{
    //claimValueTypes.Add("Our Custom Value Type");      
}

Override the FillClaimsForEntity method to add your custom entities.

protected override void FillClaimsForEntity(Uri context, SPClaim entity, List<SPClaim> claims)
{
    // claims.Add("Our Custom Entity");
}

Override the FillEntityTypes method to add your custom entity type.

protected override void FillEntityTypes(List<string> entityTypes)
{
    entityTypes.Add(SPClaimEntityTypes.FormsRole);
}

Override the FillResolve method to add your custom resolve claim.

protected override void FillResolve(Uri context, string[] entityTypes, SPClaim resolveInput, List<Microsoft.SharePoint.WebControls.PickerEntity> resolved)
{
    //PickerEntity pe = GetPickerEntity(resolvedClaim.ClaimType, resolvedClaim.ClaimValue, resolvedClaim.ClaimValueType);
    //Add it to the return list of picker entries.
    //resolved.Add(pe);
}
protected override void FillResolve(Uri context, string[] entityTypes, string resolveInput, List<Microsoft.SharePoint.WebControls.PickerEntity> resolved)
{
    //PickerEntity pe = GetPickerEntity(resolvedClaim.ClaimType, resolvedClaim.ClaimValue, resolvedClaim.ClaimValueType);
    //Add it to the return list of picker entries.
    //resolved.Add(pe);
}

Override the FillSchema method to add your custom schemas.

protected override void FillSchema(Microsoft.SharePoint.WebControls.SPProviderSchema schema)
{
    //add the schema element we need at a minimum in our picker node
    schema.AddSchemaElement(new SPSchemaElement(PeopleEditorEntityDataKeys.DisplayName, PickerEntitySchemaDisplayName, SPSchemaElementType.Both));
}

Override the FillSearch method to add your custom search claim.

protected override void FillSearch(Uri context, string[] entityTypes, string searchPattern, string hierarchyNodeID, int maxCount, Microsoft.SharePoint.WebControls.SPProviderHierarchyTree searchTree)
{
    // Nodes where we will stick our matches.
    // Microsoft.SharePoint.WebControls.SPProviderHierarchyNode matchNode = null;
    // Add the match to our node.
    // matchNode.AddEntity(pe);
}

Add the SharePoint Feature to this project and set it as Farm level.

Farm level

Deploy this solution to the site.

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.

Check if the custom Claims Provider is populated in the assigning permission to an item.

Share

Summary

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