0

I want to import some documents to a SharePoint document library. I know how to import into this library, but the problem is with user rights. I have to many documents to import so, the process has to be automatically. I have also the metadata for the documents. I am using another system which allows me to export the user rights for every document in a file. It can be CSV, excel etc, so the format is not a problem. Is there any way I can import these user rights into SharePoint?

  • Do you have access to PowerShell for the 2013 farm? – Jeff Freeman Jul 17 '14 at 14:35
  • yes, I am the main admin currently I can access everything – el94 Jul 17 '14 at 14:37
1

I understand that you have a few documents, and a CSV of users with their associated permissions for those documents. You want to upload them into SharePoint 2013.

Since each document has unique permissions, we need to set item-level permissions to each. Write a script to upload the documents to the SPList (there are a couple of ways to do this)

Then, for each file,

  1. break the role inheritance for the uploaded file

  2. create a SPRoleAssignment for each user

  3. add a SPRoleDefinition with the pertinent user permissions to the SPRoleAssignment

  4. add the SPRoleAssignment to the SPListItem.RoleAssignments

Be sure to update the SPList after you finish assigning user roles. Also note that there is a limit of 50,000 "scopes" (or, number of ACLs) per document library (Ref)

Add-SPListItem will upload your files to your SPDocumentLibrary. I separated it from the role permissions because the other code is untested and may fail. For each file, you need to identify a user's logon name, and then create a SharePoint-specific CSV for the base permissions. More details are in the code. Once you have both of those, you can then get a role assignment with Get-SPRoleAssignment and then pipe that into Add-SPRoleAssignmentToSPListItem, which should give you what you want.

Please note that I haven't tested this in my environment and you may need to do some troubleshooting to get it to work completely. Please let me know if it fails or if parts of it are unclear so I can update it!

Function Add-SPListItem
(
    [string] $webUrl,       # The URL to the SPWeb
    [string] $listTitle,    # The title of the SPDocumentLibrary
    [string] $filePath,     # The local path to the file
    [string] $itemTitle     # The title for the uploaded SPListItem
)
{
    # Getting a reference to the list
    $web = Get-SPWeb $webUrl;
    $list = $web.Lists[$listTitle];

    # Getting the SPFileCollection for the list
    $files = $list.RootFolder.Files;

    # Read the entire file into an array of bytes
    $file = [System.IO.File]::ReadAllBytes($filePath);

    # Add (Overwrite) the file with the specified name
    $item = $files.Add($itemTitle, $file, $true);

    # Disposing of the SPWeb
    $web.Dispose();
}

# Returns [Microsoft.SharePoint.SPRoleAssignment]
Function Get-SPRoleAssignment
(
    [string] $webUrl,       # The URL to the SPWeb
    [string] $logonName,    # Fully qualified user logon name "CONTOSO\user1"
    [string] $permissions   # This is a comma delimited permissions list
)
{
    # Getting a reference to the SPWeb
    $web = Get-SPWeb $webUrl;

    # Getting the SPUser (which extends SPPrincipal)
    $user = $web.EnsureUser($logonName);

    # Getting the SPRoleAssignment and its SPRoleDefinitionBindingCollection
    $roleAssignment = New-Object Microsoft.SharePoint.SPRoleAssignment($user);
    $roleDefinitionBindingsCollection = $roleAssignment.RoleDefinitionBindings;

    # Right now, the $roleDefinitionBindingsCollection will be a default 
    # collection, to which we can now add whatever base permissions we want
    $roleDefinition = New-Object Microsoft.SharePoint.SPRoleDefinition;

    # The $permissions string is a comma delimited list of enum values 
    # from SPBasePermissions.  An example would be:
    # $permissions = "ViewListItems, AddListItems, Open, ViewPages";

    # To see the list of all the enum values, use 
    # [System.Enum]::GetNames("Microsoft.SharePoint.SPBasePermissions");
    $roleDefinition.BasePermissions = $permissions;
    $roleDefinitionBindingsCollection.Add($roleDefinition);
    $roleAssignment.Update();

    # Disposing of the SPWeb
    $web.Dispose();

    return $roleAssignment;
}

Function Add-SPRoleAssignmentToSPListItem
(
    [string] $webUrl,
    [string] $listTitle,
    [string] $itemTitle,
    [Microsoft.SharePoint.SPRoleAssignment] $roleAssignment
)
{
    # Getting a reference to the list item
    $web = Get-SPWeb $webUrl;
    $list = $web.Lists[$listTitle];
    $item = $list.Items | Where { $_.Title -eq $itemTitle };

    # Adding the SPRoleAssignment to the collection of role assignments
    $item.SPRoleAssignments.Add($roleAssignment);
    $item.UpdateOverwriteVersion();

    # Disposing of the SPWeb
    $web.Dispose();
}
  • Great answer THank you for your help I will start working on it asap – el94 Jul 18 '14 at 7:34
  • is there any way I could do for all files or any other script that I can make to trigger these scripts automatically since I have to many files ? – el94 Jul 18 '14 at 7:43
  • You'll need to write the script to fit your business requirements. Try to make it work for one file and then repeat for the rest – Jeff Freeman Jul 19 '14 at 2:24

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.