示例:为实体启用字段安全性
发布日期: 2017年1月
适用于: Dynamics 365 (online),Dynamics 365 (on-premises),Dynamics CRM 2016,Dynamics CRM Online
此示例代码适用于 Microsoft Dynamics 365(在线或本地)。下载 Microsoft Dynamics CRM SDK 包。 它可能位于下载包的以下位置:
SampleCode\CS\FieldSecurity\EnableFieldSecurityForAnEntity.cs
要求
有关运行此 SDK 中提供的示例代码的要求的详细信息,请参阅使用示例和帮助程序代码。
示例
此示例演示的是如何为实体启用字段安全性。
// Connect to the Organization service.
// The using statement assures that the service proxy will be properly disposed.
using (_serviceProxy = new OrganizationServiceProxy(serverConfig.OrganizationUri, serverConfig.HomeRealmUri,serverConfig.Credentials, serverConfig.DeviceCredentials))
{
// This statement is required to enable early-bound type support.
_serviceProxy.EnableProxyTypes();
CreateRequiredRecords();
// Create Field Security Profile.
FieldSecurityProfile managersProfile = new FieldSecurityProfile();
managersProfile.Name = "Managers";
_profileId = _serviceProxy.Create(managersProfile);
Console.Write("Created Profile, ");
// Create the request object and set the monikers with the
// teamprofiles_association relationship.
AssociateRequest teamToProfile = new AssociateRequest
{
Target = new EntityReference(FieldSecurityProfile.EntityLogicalName, _profileId),
RelatedEntities = new EntityReferenceCollection
{
new EntityReference(Team.EntityLogicalName, _teamId)
},
Relationship = new Relationship("teamprofiles_association")
};
// Execute the request.
_serviceProxy.Execute(teamToProfile);
// Create custom activity entity.
CreateEntityRequest req = new CreateEntityRequest()
{
Entity = new EntityMetadata
{
LogicalName = "new_tweet",
DisplayName = new Label("Tweet", 1033),
DisplayCollectionName = new Label("Tweet", 1033),
OwnershipType = OwnershipTypes.UserOwned,
SchemaName = "New_Tweet",
IsActivity = true,
IsAvailableOffline = true,
IsAuditEnabled = new BooleanManagedProperty(true),
IsMailMergeEnabled = new BooleanManagedProperty(false)
},
HasActivities = false,
HasNotes = true,
PrimaryAttribute = new StringAttributeMetadata()
{
SchemaName = "Subject",
LogicalName = "subject",
RequiredLevel = new AttributeRequiredLevelManagedProperty(AttributeRequiredLevel.None),
MaxLength = 100,
DisplayName = new Label("Subject", 1033)
}
};
// Execute the request.
_serviceProxy.Execute(req);
Console.Write("Entity Created, ");
// Create custom attributes.
CreateAttributeRequest attrReq = new CreateAttributeRequest()
{
Attribute = new StringAttributeMetadata()
{
LogicalName = "new_identity",
DisplayName = new Label("Identity", 1033),
SchemaName = "New_Identity",
MaxLength = 500,
RequiredLevel = new AttributeRequiredLevelManagedProperty(AttributeRequiredLevel.Recommended),
IsSecured = true
},
EntityName = "new_tweet"
};
// Execute the request.
CreateAttributeResponse identityAttributeResponse = (CreateAttributeResponse)_serviceProxy.Execute(attrReq);
_identityId = identityAttributeResponse.AttributeId;
Console.Write("Identity Created, ");
attrReq = new CreateAttributeRequest()
{
Attribute = new StringAttributeMetadata()
{
LogicalName = "new_message",
DisplayName = new Label("Message", 1033),
SchemaName = "New_Message",
MaxLength = 140,
RequiredLevel = new AttributeRequiredLevelManagedProperty(AttributeRequiredLevel.Recommended),
IsSecured = true
},
EntityName = "new_tweet"
};
// Execute the request.
CreateAttributeResponse messageAttributeResponse = (CreateAttributeResponse)_serviceProxy.Execute(attrReq);
_messageId = messageAttributeResponse.AttributeId;
Console.Write("Message Created, ");
// Create the field permission for the Identity attribute.
FieldPermission identityPermission = new FieldPermission()
{
AttributeLogicalName = "new_identity",
EntityName = "new_tweet",
CanRead = new OptionSetValue(FieldPermissionType.Allowed),
FieldSecurityProfileId = new EntityReference(FieldSecurityProfile.EntityLogicalName, _profileId)
};
// Execute the request
_identityPermissionId = _serviceProxy.Create(identityPermission);
Console.Write("Permission Created. ");
DeleteRequiredRecords(promptforDelete);
}
另请参阅
IOrganizationService
在 Microsoft Dynamics 365 中,如何将字段安全用于控制访问字段值
字段安全性实体
示例:检索共享记录字段
Microsoft Dynamics 365
© 2017 Microsoft。 保留所有权利。 版权