Thursday, August 22, 2013

Create "Shared with Everyone" folder in Sky Drive Pro / Documents library - SharePoint 2013

Introduction

As I mentioned in my previous post our team faced issues in document migration from SharePoint 2010 to SharePoint 2013. The actions were not as expected.

Problem Background

In some profile we couldn't find "Shared with Everyone" which folder is having "Read" permission to "All Authenticated Users". So we created a console application which will create "Shared with Everyone" folder and set the permission as we needed.

Solution

Please pass your personal site (spWeb) to the method.

   
 public static void CreateSharedFolder(SPWeb web)
 {

    SPList list = (SPDocumentLibrary)web.Lists["Documents"];
    //Get the folders inside Documents library
    SPFolderCollection folders = web.GetFolder(list.RootFolder.Url).SubFolders;
    List<string> folderList = new List<string>();
    //Add the SPFolderCollection to a generic list
    foreach (SPFolder folder in folders)
    {
        folderList.Add(folder.Name);
    }
    //Check whether folder list has shared with everyone folder
    if (!folderList.Contains("Shared with Everyone"))
    {

       //Create folder inside Documents library 
       folders.Add("Shared with Everyone");

       //Greate users
       SPUser allUser = web.EnsureUser("NT AUTHORITY\\authenticated users");
       //Get folder
       SPListItem item = list.Folders[0];
       if (!item.HasUniqueRoleAssignments)
           item.BreakRoleInheritance(true);

       SPRoleAssignment roleAssignment = new SPRoleAssignment(allUser);
       SPRoleDefinition roleDefination = web.RoleDefinitions["Read"];
                //Add role assignment
       roleAssignment.RoleDefinitionBindings.Add(roleDefination);
       item.RoleAssignments.Add(roleAssignment);
       web.AllowUnsafeUpdates = true;
       item.Update();
       web.AllowUnsafeUpdates = false;

    }

}
 
 


Conclution
Now you can see "Shared with Everyone" folder inside SkyDrive pro.

No comments:

Post a Comment