Let me first preface by saying that I am new to programming with the SharePoint Object Model...

I have a requirement where after a list item is created, which indicates 2 individuals in a person lookup, I need to create a new folder in a document library and grant permission to folder to the users in the person lookup fields. I have created an event receiver to create the new folder and edit some of the list item fields accordingly. My problem is that I am unsure as how grant permission to the users indicated in the person lookup field after creating the folder. We need to grant permission only to the individual users and not any groups they may be a part of. These permissions should be added to the current permissions on the document library. Here is the code I have so far.

public override void ItemAdded(SPItemEventProperties properties)

        const string SOURCE_DIRECTORY = @"http://sitecoll/Forms/SourceLibrary/";
        const string DESTINATION_DIRECTORY = @"http://sitecoll/site/subsite/DestLibrary/";

            // Get inserted item and update its fields
            SPListItem insertedItem = properties.ListItem;
            string destFolderName = insertedItem.UniqueId.ToString() + @"/";
            insertedItem["Title"] = properties.UserDisplayName;
            insertedItem["URL"] = DESTINATION_DIRECTORY + destFolderName;

            Uri srcUrl = new Uri(SOURCE_DIRECTORY);
            Uri dstFolderUrl = new Uri(DESTINATION_DIRECTORY);
            Uri dstNewFolderUrl = new Uri(dstFolderUrl + destFolderName);
            Uri srcFileUrl = new Uri(SOURCE_DIRECTORY + "filename.pdf");

            using (SPSite siteSrc = new SPSite(srcUrl.AbsoluteUri))
            using (SPSite siteDst = new SPSite(dstFolderUrl.AbsoluteUri))
            using (SPWeb webSrc = siteSrc.OpenWeb())
            using (SPWeb webDst = siteDst.OpenWeb())
                // Create New Folder in QM/Exposure/ Library
                SPFolder dstFolder = webDst.GetFolder(dstFolderUrl.AbsolutePath);
                SPFolder newFolder = dstFolder.SubFolders.Add(dstNewFolderUrl.AbsolutePath);

                // Add File to new folder (do for each file)
                SPFile srcFile = webSrc.GetFile(srcFileUrl.AbsolutePath);
                newFolder.Files.Add(dstNewFolderUrl.AbsolutePath + srcFile.Name, srcFile.OpenBinary());

                // TODO Add Permissions to newFolder using 'Employee' and 'Manager' person lookup fields

        catch (Exception exc)
            properties.ErrorMessage = exc.Message;
            properties.Status = SPEventReceiverStatus.CancelWithError;

Any help on this would be greatly appreciated. Thanks!

  • if you got the solution then please post that as an answer, do not append it in the question itself, if you post as an answer then it will definitely helpful to other :-) – Aakash Maurya May 17 '16 at 19:16
  • @Aakash Morya - I have moved the solution to an answer. Thanks! – Pat Coombe May 17 '16 at 19:53

SOLVED: After lengthy research I was able to come up with a working solution.

               // Break Inheritance from Parent library

               // Remove 'Visitor' group from permissions
               SPPrincipal principal = (SPPrincipal)webDst.SiteGroups["Visitor"];

               // Get SPUSer for employee
               SPFieldUser employeeField = (SPFieldUser)insertedItem.Fields.GetField("Employee");
               SPFieldUserValue employeeFieldValue = (SPFieldUserValue)employeeField.GetFieldValue(insertedItem["Employee"].ToString());
               SPUser employee = employeeFieldValue.User;

               // Get SPUser for manager
               SPFieldUser managerField = (SPFieldUser)insertedItem.Fields.GetField("Manager");
               SPFieldUserValue managerFieldValue = (SPFieldUserValue)managerField.GetFieldValue(insertedItem["Manager"].ToString());
               SPUser manager = managerFieldValue.User;

               // Get Visitor roles
               SPRoleDefinition visitor = webDst.RoleDefinitions["Visitor"];
               SPRoleDefinition visitorSpc = webDst.RoleDefinitions["VisitorSpc"];
               SPRoleDefinition visitorExt = webDst.RoleDefinitions["VisitorExt"];

               // Grant employee all Visitor permissions
               SPRoleAssignment employeeRole = new SPRoleAssignment(employee);

               // Grant manager all Visitor permissions
               SPRoleAssignment managerRole = new SPRoleAssignment(manager);

               // Update folder

               // Update Library

Hope this helps others.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.