DISQUS

Agile SharePoint Development by 21apps: Create Custom Policy programmatically

  • Ven · 1 year ago
    Hi,
    I am trying to get the users from the "Policy of Web Application" in Central Administration through Object Model

    and also trying to add users to the same Policy through object model. I am using the following code to achieve

    this. But I am getting an "Object Reference Not Set to an Instance of an Object" exception when I am trying to call

    the Update Method of the SPWebApplication object.
    Please help me. The strange thing happening here is,when I am trying to add the users to the WebApplication policy,

    the users are getting added to though the update fails. The next time when I debug the application, I do see the

    new users added though the update failed. Moreover, I am not able to see the newly added users in the "Policy For

    Web Application" section of the Central Administration. Can anyone please help me out. Here is the code which I am

    using to achieve this.

    SPFarm farm = SPFarm.Local;
    SPWebService service = farm.Services.GetValue("");
    foreach(SPWebApplication webApp in service.WebApplications)
    {
    if(webApp.Name == "My Web App")
    {
    webApp.ZonePolicies(SPUrlZone.Default).Add("username","displayname");
    webApp.Update(); // I am getting the Object Reference error at this line
    }
    }


    Please help me ASAP.
  • Andrew Woodward · 1 year ago
    Ven,
    The reason is probably due to you not setting any policy bindings, as in the post example:-

    policy.PolicyRoleBindings.Add(oWebApplication.PolicyRoles.GetSpecialRole(SPPolicyRoleType.FullControl));