1

I need to add a Button which will have its own logic of creating and sharing a new folder.

1st the requirement:

  1. Click New > Click Create Folder (custom Button see image 1)
    • The Custom Form appears with addition required details to fill
      • Add Folder Name
      • Add People to Share With (existing users or new emails)
      • Add Expiry Date (optional)
    • Click Save (this would trigger the Script attached bellow to create the folder and share)
      • Folder is created
      • Sub-folders are created (predefined naming) *Image 2 and 3

Please let me know if we are on the right track or there is another way of doing the same

Code used for the process:

public void ChangeFolderPermission3()
     {
      SPClient.ClientContext context = new 
     SPClient.ClientContext("https://oursite.sharepoint.com/sites/sitename");
                System.Security.SecureString passWord = new System.Security.SecureString();
                foreach (char c in "passwordhere".ToCharArray()) passWord.AppendChar(c);
                context.Credentials = new SharePointOnlineCredentials("myuser@ourdomain.com", passWord);
                //SPClient.Principal _user = context.Web.EnsureUser(@"newfellow@ourdomain.com");
                SPClient.Web thisWeb = context.Web;

                Site site = context.Site;
                List list = thisWeb.Lists.GetByTitle("Documents");
                Folder newFolder = list.RootFolder.Folders.Add("ProjectFolder");
                context.ExecuteQuery();
                newFolder.ListItemAllFields.BreakRoleInheritance(false, true);
                var role = new RoleDefinitionBindingCollection(context);
                role.Add(thisWeb.RoleDefinitions.GetByType(RoleType.Contributor));
                User user = thisWeb.EnsureUser(@"newfellow@ourdomain.com");
                newFolder.ListItemAllFields.RoleAssignments.Add(user, role);
                newFolder.Update();
                context.ExecuteQuery();
                MessageBox.Show("Permissions Granted Successfully ! ");
            }

Images:

Where the Custom Form Button would Be

Root Folder Created once the form Was saved with details

Sub-Folders Created

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.