Ejemplo 2: escritura de un proveedor de notificaciones
Última modificación: martes, 03 de agosto de 2010
Hace referencia a: SharePoint Foundation 2010
Escritura de un proveedor de notificaciones
En el ejemplo siguiente se describe cómo escribir un proveedor de notificaciones. Esta implementación de ejemplo admite entidades, resoluciones y búsquedas, pero no jerarquías. Puede ver un ejemplo de código que refleja cómo admitir jerarquías en el tema de referencia del método FillHierarchy() de la clase SPClaimProvider.
Para escribir un proveedor de notificaciones, el primer paso consiste en crear una clase que derive de la clase SPClaimProvider. En este tema se supone que ha leído el tema Procedimiento para implementar un proveedor de notificaciones.
Para obtener más información acerca de cómo crear un proveedor de notificaciones y realizar un tutorial sobre este tema, vea el tutorial para escribir un proveedor de notificaciones para SharePoint 2010.
Sugerencia |
---|
Para ver otro ejemplo de código y obtener más información acerca de la clase SPClaimProvider y sus miembros, vea SPClaimProvider. Asimismo, revise el blog del equipo de SharePoint SPIdentity y el blog de Share-n-dipity periódicamente para obtener ejemplos y actualizaciones adicionales. |
Ejemplo de código proporcionado por: Andy Li, Microsoft Corporation.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration.Claims;
using Microsoft.IdentityModel.Claims;
using System.Collections;
using Microsoft.SharePoint.WebControls;
namespace ContosoClaimProviders
{
public class CRMClaimProvider : SPClaimProvider
{
public CRMClaimProvider(string displayName)
: base(displayName)
{
}
public override string Name
{
get { return "ContosoCRMClaimProvider"; }
}
/// <summary>
/// Must return true if you are doing claim augmentation.
/// </summary>
public override bool SupportsEntityInformation
{
get { return true; }
}
/// <summary>
/// Return true if you support claim resolve in the People Picker control.
/// </summary>
public override bool SupportsResolve
{
get { return true; }
}
/// <summary>
/// Return true if you support claim search in the People Picker control.
/// </summary>
public override bool SupportsSearch
{
get { return true; }
}
/// <summary>
/// Return true if you support hierarchy display in the People Picker control.
/// </summary>
public override bool SupportsHierarchy
{
get { return false; }
}
public override bool SupportsUserSpecificHierarchy
{
get
{
return base.SupportsUserSpecificHierarchy;
}
}
/// <summary>
/// Implement this method if the claims provider supports claim augmentation.
/// </summary>
/// <param name="context"></param>
/// <param name="entity"></param>
/// <param name="claims"></param>
protected override void FillClaimsForEntity(Uri context, SPClaim entity, List<SPClaim> claims)
{
if (null == entity)
{
throw new ArgumentNullException("entity");
}
if (null == claims)
{
throw new ArgumentNullException("claims");
}
//Adding the role claim.
SPClaim userIdClaim = SPClaimProviderManager.DecodeUserIdentifierClaim(entity);
//Adding claims for user.
List<string> allCRMUsers = CRMUserInfo.GetAllUsers();
if(allCRMUsers.Contains(userIdClaim.Value.ToLower()))
{
List<Claim> userClaims = CRMUserInfo.GetClaimsForUser(userIdClaim.Value.ToLower());
foreach(Claim claim in userClaims)
{
claims.Add(CreateClaim(claim.ClaimType, claim.Value, claim.ValueType));
}
}
}
/// <summary>
/// Returns all the claim types that are supported by this claims provider.
/// </summary>
/// <param name="claimTypes"></param>
protected override void FillClaimTypes(List<string> claimTypes)
{
if (null == claimTypes)
{
throw new ArgumentNullException("claimTypes");
}
// Add the claim types that will be added by this claims provider.
claimTypes.Add(CRMClaimType.Role);
claimTypes.Add(CRMClaimType.Region);
}
/// <summary>
/// Return all claim value types that correspond to the claim types.
/// You must return the values in the same order as in the FillClaimTypes() method.
/// </summary>
/// <param name="claimValueTypes"></param>
protected override void FillClaimValueTypes(List<string> claimValueTypes)
{
if (null == claimValueTypes)
{
throw new ArgumentNullException("claimValueTypes");
}
claimValueTypes.Add(Microsoft.IdentityModel.Claims.ClaimValueTypes.String);
claimValueTypes.Add(Microsoft.IdentityModel.Claims.ClaimValueTypes.String);
}
/// <summary>
/// Required for People Picker. This tells the People Picker what
/// information is available for the entity.
/// </summary>
/// <param name="schema"></param>
protected override void FillSchema(SPProviderSchema schema)
{
schema.AddSchemaElement(new SPSchemaElement(PeopleEditorEntityDataKeys.DisplayName,
"DisplayName",
SPSchemaElementType.TableViewOnly));
}
/// <summary>
/// Returns the entity type for the claims returned from the claims provider.
/// </summary>
/// <param name="entityTypes"></param>
protected override void FillEntityTypes(List<string> entityTypes)
{
entityTypes.Add(SPClaimEntityTypes.FormsRole);
entityTypes.Add(SPClaimEntityTypes.FormsRole);
}
/// <summary>
/// Required if you implement the claim search for the People Picker control.
/// </summary>
/// <param name="context"></param>
/// <param name="entityTypes"></param>
/// <param name="searchPattern"></param>
/// <param name="hierarchyNodeID"></param>
/// <param name="maxCount"></param>
/// <param name="searchTree"></param>
protected override void FillSearch(Uri context, string[] entityTypes, string searchPattern, string hierarchyNodeID, int maxCount, SPProviderHierarchyTree searchTree)
{
string keyword = searchPattern.ToLower();
Hashtable knownClaims = CRMUserInfo.GetAllClaims();
List<string> knownClaimsList = new List<string>();
//Convert the knownClaims.Key into a List<string> for LINQ query.
foreach (string claim in knownClaims.Keys)
{
knownClaimsList.Add(claim);
}
var claimQuery = knownClaimsList.Where(claim => claim.IndexOf(keyword) >= 0).Select(claim => claim);
foreach (string claimValue in claimQuery)
{
//Get the claim type.
//For example, if you are search "SalesManager", the ClaimType will be
//CRMClaimType.Role
string claimType = CRMUserInfo.GetClaimTypeForRole((string)knownClaims[claimValue]);
PickerEntity entity = CreatePickerEntity();
entity.Claim = CreateClaim(claimType, claimValue, Microsoft.IdentityModel.Claims.ClaimValueTypes.String);
entity.Description = claimValue;
entity.DisplayText = claimValue;
entity.EntityData[PeopleEditorEntityDataKeys.DisplayName] = claimValue;
entity.EntityType = SPClaimEntityTypes.FormsRole;
entity.IsResolved = true;
searchTree.AddEntity(entity);
}
}
/// <summary>
/// Resolve one single claim by using exact match. This method is required for both
/// claim search and resolve.
/// </summary>
/// <param name="context"></param>
/// <param name="entityTypes"></param>
/// <param name="resolveInput"></param>
/// <param name="resolved"></param>
protected override void FillResolve(Uri context, string[] entityTypes, SPClaim resolveInput, List<PickerEntity> resolved)
{
string keyword = resolveInput.Value.ToLower();
Hashtable knownClaims = CRMUserInfo.GetAllClaims();
if (knownClaims.ContainsKey(keyword))
{
//Get the claim type.
//For example, if you are search "SalesManager", the ClaimType will be
// CRMClaimType.Role. In this case, the keyword is the value of the claim.
string claimValue = keyword;
string claimType = CRMUserInfo.GetClaimTypeForRole((string)knownClaims[keyword]);
PickerEntity entity = CreatePickerEntity();
entity.Claim = CreateClaim(claimType, claimValue, Microsoft.IdentityModel.Claims.ClaimValueTypes.String);
entity.Description = claimValue;
entity.DisplayText = claimValue;
entity.EntityData[PeopleEditorEntityDataKeys.DisplayName] = claimValue;
entity.EntityType = SPClaimEntityTypes.FormsRole;
entity.IsResolved = true;
resolved.Add(entity);
}
}
/// <summary>
/// Required if you implement claim resolve for the People Picker control.
/// </summary>
/// <param name="context"></param>
/// <param name="entityTypes"></param>
/// <param name="resolveInput"></param>
/// <param name="resolved"></param>
protected override void FillResolve(Uri context, string[] entityTypes, string resolveInput, List<PickerEntity> resolved)
{
string keyword = resolveInput.ToLower();
Hashtable knownClaims = CRMUserInfo.GetAllClaims();
List<string> knownClaimsList = new List<string>();
//Convert the knownClaims.Key into a List<string> for LINQ query.
foreach (string claim in knownClaims.Keys)
{
knownClaimsList.Add(claim);
}
var claimQuery = knownClaimsList.Where(claim => claim.IndexOf(keyword) >= 0).Select(claim => claim);
foreach (string claimValue in claimQuery)
{
//Get the claim type.
//For example, if you search "SalesManager", the ClaimType will be
//CRMClaimType.Role
string claimType = CRMUserInfo.GetClaimTypeForRole((string)knownClaims[claimValue]);
PickerEntity entity = CreatePickerEntity();
entity.Claim = CreateClaim(claimType, claimValue, Microsoft.IdentityModel.Claims.ClaimValueTypes.String);
entity.Description = claimValue;
entity.DisplayText = claimValue;
entity.EntityData[PeopleEditorEntityDataKeys.DisplayName] = claimValue;
entity.EntityType = SPClaimEntityTypes.FormsRole;
entity.IsResolved = true;
resolved.Add(entity);
}
}
protected override void FillHierarchy(Uri context, string[] entityTypes, string hierarchyNodeID, int numberOfLevels, SPProviderHierarchyTree hierarchy)
{
throw new NotImplementedException();
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ContosoClaimProviders
{
public class CRMClaimType
{
public static string Role = "http://schemas.sample.org/ws/2009/12/identity/claims/CRMRole";
public static string Region = "http://schemas.sample.org/ws/2009/12/identity/claims/CRMRegion";
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint.Administration;
using Microsoft.IdentityModel.Claims;
using System.Collections;
namespace ContosoClaimProviders
{
public class CRMUserInfo
{
/// <summary>
/// A real-world implementation should look up a directory service or database
/// to retrieve a user’s claim.
/// The code below is used only for demostration purposes.
/// </summary>
/// <param name="username"></param>
/// <returns></returns>
public static List<Claim> GetClaimsForUser(string username)
{
List<Claim> userClaims = new List<Claim>();
foreach(string userInfo in userDB)
{
string[] claims = userInfo.Split(new string[] { ":" }, StringSplitOptions.RemoveEmptyEntries);
if (username == claims[0])
{
userClaims.Add(new Claim(GetClaimTypeForRole(claims[1]), claims[2], Microsoft.IdentityModel.Claims.ClaimValueTypes.String));
}
}
return userClaims;
}
//Manually construct a list of users. In a real-world production environment,
//you should look up a directory serivce or database to retrieve the user information.
public static List<string> GetAllUsers()
{
List<string> allUsers = new List<string>();
//Adding Forms users.
allUsers.Add("bob");
allUsers.Add("mary");
allUsers.Add("jack");
allUsers.Add("admin1");
//Add Windows domain user, if you want this provider
//to support Windows claim mode.
allUsers.Add("contoso\\myalias");
return allUsers;
}
/// <summary>
/// This function returns all the known claims from the CRM system so that
/// the claims provider is able to search and resolve
/// the claims in the People Picker control.
/// </summary>
/// <returns></returns>
public static Hashtable GetAllClaims()
{
Hashtable knownClaims = new Hashtable();
foreach(string claimItem in claimsDB)
{
string[] claim = claimItem.Split(new string[] { ":" }, StringSplitOptions.RemoveEmptyEntries);
knownClaims.Add(claim[1].ToLower(), claim[0].ToLower());
}
return knownClaims;
}
public static string GetClaimTypeForRole(string roleName)
{
if (roleName.Equals("CRMRole", StringComparison.OrdinalIgnoreCase))
return CRMClaimType.Role;
else if (roleName.Equals("CRMRegion", StringComparison.OrdinalIgnoreCase))
return CRMClaimType.Region;
else
throw new Exception("CRM Claim Type not found!");
}
private static string[] userDB =
{
"bob:CRMRole:Reader",
"bob:CRMRole:SalesRepresentative",
"bob:CRMRegion:NorthWest",
"mary:CRMRole:Reader",
"mary:CRMRole:SalesManager",
"mary:CRMRegion:East",
"jack:CRMRole:Reader",
"jack:CRMRole:Executive",
"jack:CRMRegion:East",
"admin1:CRMRole:Administrator",
"contoso\\myalias:CRMRole:SalesManager"
};
private static string[] claimsDB =
{"CRMRole:Reader",
"CRMRole:SalesRepresentative",
"CRMRole:SalesManager",
"CRMRole:Executive",
"CRMRole:Administrator",
"CRMRegion:NorthWest",
"CRMRegion:East",
};
}
}