SPPolicyCollection Class
Represents a collection of SPPolicy objects.
Microsoft.SharePoint.Administration.SPAutoSerializingObject
Microsoft.SharePoint.Administration.SPPolicyCollection
Assembly: Microsoft.SharePoint (in Microsoft.SharePoint.dll)
Available in Sandboxed Solutions: No
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:
-
Use the Add method to create a policy object within the collection of policies for the Web application.
-
Use the Add method of the SPPolicyRoleCollection class to create a policy role object within the collection of policy roles for the Web application.
-
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.
-
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.
The following example creates a new policy within the collection of policies of a specified Web application.
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();