共用方式為


SPPolicyCollection Class

Represents a collection of SPPolicy objects.

Inheritance Hierarchy

System.Object
  Microsoft.SharePoint.Administration.SPAutoSerializingObject
    Microsoft.SharePoint.Administration.SPPolicyCollection

Namespace:  Microsoft.SharePoint.Administration
Assembly:  Microsoft.SharePoint (in Microsoft.SharePoint.dll)
Available in Sandboxed Solutions: No

Syntax

'Declaration
<SharePointPermissionAttribute(SecurityAction.InheritanceDemand, ObjectModel := True)> _
<SharePointPermissionAttribute(SecurityAction.LinkDemand, ObjectModel := True)> _
Public NotInheritable Class SPPolicyCollection _
    Inherits SPAutoSerializingObject _
    Implements IEnumerable
'Usage
Dim instance As SPPolicyCollection
[SharePointPermissionAttribute(SecurityAction.InheritanceDemand, ObjectModel = true)]
[SharePointPermissionAttribute(SecurityAction.LinkDemand, ObjectModel = true)]
public sealed class SPPolicyCollection : SPAutoSerializingObject, 
    IEnumerable

Remarks

The policy for a zone is a collection of policy objects. Each user or group is assigned a separate policy object.

Use the Policies property of the SPWebApplication class to return the collection of policies for a Web application, or use the ZonePolicies method of the SPWebApplication class to return the collection of policies for a specified zone.

To create a policy, perform the following steps:

  1. Use the Add method to create a policy object within the collection of policies for the Web application.

  2. Use the Add method of the SPPolicyRoleCollection class to create a policy role object within the collection of policy roles for the Web application.

  3. Use the Add method of the SPPolicy.SPPolicyRoleBindingCollection class to add the new policy role to the collection of policy bindings for the new policy.

  4. Call the Update method of the SPWebApplication class for changes to take effect.

Use an indexer to return a single policy from the collection. For example, if the collection is assigned to a variable named myPolicies, use myPolicies[index] in C#, or myPolicies(index) in Visual Basic, where index is either the name or the index number of the policy in the collection.

Examples

The following example creates a new policy within the collection of policies of a specified Web application.

Dim uri = New Uri("http://MyServer")
Dim webApplication As SPWebApplication = SPWebApplication.Lookup(uri)
Dim policyCollection As SPPolicyCollection = webApplication.Policies
Dim policyRoles As SPPolicyRoleCollection = webApplication.PolicyRoles

Dim policy As SPPolicy = policyCollection.Add("UserAlias", "MyPolicy")

Dim policyRole As SPPolicyRole = policyRoles.Add("MyPolicyRole", "My description.", SPBasePermissions.ViewPages Or SPBasePermissions.ViewListItems, SPBasePermissions.AddAndCustomizePages Or SPBasePermissions.AddListItems)

policy.PolicyRoleBindings.Add(policyRole)

webApplication.Update()
System.Uri uri = new Uri("http://MyServer");
SPWebApplication webApplication = SPWebApplication.Lookup(uri);
SPPolicyCollection policyCollection = webApplication.Policies;
SPPolicyRoleCollection policyRoles = webApplication.PolicyRoles;

SPPolicy policy = policyCollection.Add("UserAlias","MyPolicy");

SPPolicyRole policyRole = policyRoles.Add("MyPolicyRole", "My description.", SPBasePermissions.ViewPages | SPBasePermissions.ViewListItems, SPBasePermissions.AddAndCustomizePages | SPBasePermissions.AddListItems);

policy.PolicyRoleBindings.Add(policyRole);

webApplication.Update();

Thread Safety

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

See Also

Reference

SPPolicyCollection Members

Microsoft.SharePoint.Administration Namespace