Create Custom Policy programmatically
Answering a forum thread I had to do a bit of digging into how to create new Policies in code.
This post from Ethan helped point me in the right direction. The requirement was to programmatically give users Full Control of a web application, the code snippet below shows how to achieve this.
SPWebApplication oWebApplication = SPWebApplication.Lookup(new Uri(“http://webappurl”)); SPPolicy policy = oWebApplication.Policies.Add(“domain\\username”, “MyCustomPolicy”);policy.PolicyRoleBindings.Add(oWebApplication.PolicyRoles.GetSpecialRole(SPPolicyRoleType.FullControl));oWebApplication.Update();
You may have noticed that the policy only allows for one username, this is the case even though the UI gives you the option to enter multiple, only the first is stored.
