Thursday, July 3, 2008

Create an Anonymous Access Survey Programmatically

Their are several good examples on how to use the UI to make a SharePoint 2007 survey work for anonymous users.  So I took what I learned from them and came up with this method for doing the same thing programmatically.

The main catch was the AnonymousPermMask64 flags.  The only ones that you see in the UI are the AddListItems and ViewListItems.  But there are several others that are set behind the scenes.

 

 
private void AddNewSurvey(SPWeb web, SPListTemplate surveyTemplate)
{
    web.Lists.Add("Survey Title", "Survey Description", surveyTemplate);
    
    // once we have created the survey, find it again so we can set the permissions
    SPList surveyList = web.Lists["Survey Title"];
    web.AllowUnsafeUpdates = true;
    // break the role inheritance so we can set our own permissons
    if (!surveyList.HasUniqueRoleAssignments)
        surveyList.BreakRoleInheritance(true);
    surveyList.ReadSecurity = 2; // can read their own
    surveyList.WriteSecurity = 2; // can edit their own
    // set the permission flags for Add and View plus some internal flags
    surveyList.AnonymousPermMask64 =
        (SPBasePermissions.Open |
        SPBasePermissions.OpenItems |
        SPBasePermissions.ViewFormPages |
        SPBasePermissions.UseClientIntegration |
        //SPBasePermissions.UseRemoteAPIs |
        SPBasePermissions.ViewListItems |
        SPBasePermissions.AddListItems);
    surveyList.Update();
}


0 comments: