共用方式為


The Scratch Custom Claims Provider

Frequently, I find it useful to have a common starting point – what Raymond Chen describes as a “scratch program” (or a new scratch program, for those update-minded types). Within SharePoint, things are never that simple – a scratch SharePoint solution is an empty whiteboard, for better or for worse.

There are plenty of extensibility points within SharePoint that certainly offer up opportunities for scratch programs, though, and one place I find myself sharing – over and over again – my personal scratch implementation is with Custom Claims Providers. And so I submit for your approval, the SimpleClaimsProvider in all its favoriteColor issuing claim glory. This will serve as a starting point for future posts involving custom claims providers, and in the meantime might give a few of you the bits you need in order to make the details of Custom Claims Providers real. For a complete walkthrough, see Steve Peschka’s most excellent write up over on MSDN.

   1 using System;
  2 using System.Collections.Generic;
  3 using System.Linq;
  4 using System.Text;
  5 
  6 using Microsoft.SharePoint;
  7 using Microsoft.SharePoint.WebControls;
  8 using Microsoft.SharePoint.Administration;
  9 using Microsoft.SharePoint.Administration.Claims;
 10 
 11 namespace BryanPorter.SP.CCP
 12 {
 13     public class SimpleClaimsProvider
 14         : SPClaimProvider
 15     {
 16         private static string m_claimValueType = Microsoft.IdentityModel.Claims.ClaimValueTypes.String;
 17         private static string m_claimType = "https://schemas.bryanporter.com/favoriteColor";
 18 
 19         private string m_sharedState = null;
 20 
 21         public SimpleClaimsProvider(string displayName)
 22             : base(displayName)
 23         { }        
 24 
 25         protected override void FillClaimTypes(List<string> claimTypes)
 26         {
 27             if (claimTypes == null)
 28                 throw new ArgumentNullException("claimTypes");
 29 
 30             claimTypes.Add(m_claimType);
 31         }
 32 
 33         protected override void FillClaimValueTypes(List<string> claimValueTypes)
 34         {
 35             if (claimValueTypes == null)
 36                 throw new ArgumentNullException("claimValueTypes");
 37 
 38             claimValueTypes.Add(m_claimValueType);
 39         }
 40 
 41         protected override void FillClaimsForEntity(Uri context, SPClaim entity, List<SPClaim> claims)
 42         {
 43             if (entity == null)
 44                 throw new ArgumentNullException("entity");
 45 
 46             if (claims == null)
 47                 throw new ArgumentNullException("claims");
 48 
 49             claims.Add(CreateClaim(m_claimType, "Blue", m_claimValueType));
 50         }
 51 
 52         protected override void FillEntityTypes(List<string> entityTypes)
 53         {
 54             entityTypes.Add(SPClaimEntityTypes.FormsRole);
 55         }
 56 
 57         protected override void FillHierarchy(Uri context, string[] entityTypes, string hierarchyNodeID, int numberOfLevels, Microsoft.SharePoint.WebControls.SPProviderHierarchyTree hierarchy)
 58         {
 59             throw new NotImplementedException();
 60         }
 61 
 62         protected override void FillResolve(Uri context, string[] entityTypes, SPClaim resolveInput, List<Microsoft.SharePoint.WebControls.PickerEntity> resolved)
 63         {
 64             if (!EntityTypesContain(entityTypes, SPClaimEntityTypes.FormsRole))
 65                 return;
 66 
 67             PickerEntity pe = CreatePickerEntity();
 68             pe.Claim = CreateClaim(m_claimType, "Blue", m_claimValueType);
 69             pe.Description = Name + ":" + "Blue";
 70             pe.DisplayText = "Blue";
 71             pe.EntityData[PeopleEditorEntityDataKeys.DisplayName] = "Blue";
 72             pe.EntityType = SPClaimEntityTypes.FormsRole;
 73             pe.IsResolved = true;
 74             pe.EntityGroupName = "Favorite Color";
 75 
 76             resolved.Add(pe);
 77         }
 78 
 79         protected override void FillResolve(Uri context, string[] entityTypes, string resolveInput, List<Microsoft.SharePoint.WebControls.PickerEntity> resolved)
 80         {
 81             if (!EntityTypesContain(entityTypes, SPClaimEntityTypes.FormsRole))
 82                 return;
 83 
 84             if (resolveInput.ToUpper().Contains("BLUE"))
 85             {
 86                 PickerEntity pe = CreatePickerEntity();
 87                 pe.Claim = CreateClaim(m_claimType, "Blue", m_claimValueType);
 88                 pe.Description = Name + ":" + "Blue";
 89                 pe.DisplayText = "Blue";
 90                 pe.EntityData[PeopleEditorEntityDataKeys.DisplayName] = "Blue";
 91                 pe.EntityType = SPClaimEntityTypes.FormsRole;
 92                 pe.IsResolved = true;
 93                 pe.EntityGroupName = "Favorite Color";
 94 
 95                 resolved.Add(pe);
 96             }
 97 
 98         }
 99 
100         protected override void FillSchema(Microsoft.SharePoint.WebControls.SPProviderSchema schema)
101         {
102             schema.AddSchemaElement(new SPSchemaElement(PeopleEditorEntityDataKeys.DisplayName, "Display Name", SPSchemaElementType.Both));
103         }
104 
105         protected override void FillSearch(Uri context, string[] entityTypes, string searchPattern, string hierarchyNodeID, int maxCount, Microsoft.SharePoint.WebControls.SPProviderHierarchyTree searchTree)
106         {
107             if (!EntityTypesContain(entityTypes, SPClaimEntityTypes.FormsRole))
108                 return;
109 
110             if (searchPattern.ToUpper().Contains("BLUE"))
111             {
112                 PickerEntity pe = CreatePickerEntity();
113                 pe.Claim = CreateClaim(m_claimType, "Blue", m_claimValueType);
114                 pe.Description = Name + ":" + "Blue";
115                 pe.DisplayText = "Blue";
116                 pe.EntityData[PeopleEditorEntityDataKeys.DisplayName] = "Blue";
117                 pe.EntityType = SPClaimEntityTypes.FormsRole;
118                 pe.IsResolved = true;
119                 pe.EntityGroupName = "Favorite Color";
120 
121 
122                 searchTree.AddEntity(pe);
123             }
124         }
125 
126         public override string Name
127         {
128             get { return "SimpleCCP"; }
129         }
130 
131         public override bool SupportsEntityInformation
132         {
133             get { return true; }
134         }
135 
136         public override bool SupportsHierarchy
137         {
138             get { return false; }
139         }
140 
141         public override bool SupportsResolve
142         {
143             get { return true; }
144         }
145 
146         public override bool SupportsSearch
147         {
148             get { return true; }
149         }
150     }
151 }