5

I want to copy a folder from one document library to another document library and maintain permissions on the folder structure. When I CUT the folder and paste it the permissions on the folder structure are maintained. When I want to COPY the folder and paste it, the folder inherits the permissions all over again. How can I resolve this?

Does somebody have an answer to this?

4

This is a powershell script which copies all the folders and its content from one list(source list) to a new list (dest list) with required permissions.

Remove-PSSnapin Microsoft.SharePoint.PowerShell -erroraction SilentlyContinue
Add-PSSnapin Microsoft.SharePoint.PowerShell -erroraction SilentlyContinue

function Copy-ItemsToFolder([Microsoft.SharePoint.SPFolder]$folder, $srcList, $destList)
{
$spQuery = New-Object Microsoft.SharePoint.SPQuery
$spQuery.Folder = $folder


$sourceSPListItemCollection = $srcList.GetItems($spQuery);
$destFolder = $destList.Folders[$folder.Name];
if ($destFolder -eq $null -or $destFolder -eq "")
{
    #folder does not exist so create it.
    $destFolder = $destList.AddItem("", [Microsoft.SharePoint.SPFileSystemObjectType]::Folder)
    $destFolder["Title"] = $folder.Name;
    $destFolder.Update()
}
$count = $sourceSPListItemCollection.Count;
$index = 0;
$newFolderPath = $destFolder.Folder.ServerRelativeUrl
foreach($srcListItem in $sourceSPListItemCollection)
{
    $index++;

    write-host "adding new item $index of $count";
    $newSPListItem = $destList.AddItem($newFolderPath, [Microsoft.SharePoint.SPFileSystemObjectType]::File, $null);

    foreach($spField in $srcListItem.Fields)
    {
        if ($spField.ReadOnlyField -ne $True -and $spField.InternalName -ne "Attachments")
        {
            $newSPListItem[$spField.InternalName] = $srcListItem[$spField.InternalName];
        }
    }
    $newSPListItem.Update()
    write-host "added";
}
}
try
{
$srcListSiteUrl = "https://sourcesite/ad/"
$SourceListName = "SiteSourceList"

$dstListSiteUrl = "https://destsite/BgP/"
$DestinationListName = "SiteDestArchive"

$keyColumnInternalName = "Title"

$sourceListWeb = Get-SPWeb -identity $srcListSiteUrl
$sourceListUrl = $sourceListWeb.ServerRelativeUrl + "/lists/" + $SourceListName;

$dstListWeb = Get-SPWeb -identity $dstListSiteUrl
$destinationListUrl = $dstListWeb.ServerRelativeUrl + "/lists/" + $DestinationListName;

$SourceList = $sourceListWeb.GetList($sourceListUrl);
$DestinationList = $dstListWeb.GetList($destinationListUrl);

foreach($currentFolder in $SourceList.RootFolder.SubFolders)
{
    Write-Host "processing folder $($currentFolder.Name)";

    if ($currentFolder.ItemCount -gt 0)
    {
        Copy-ItemsToFolder $currentFolder $SourceList $DestinationList
    }
    Write-Host "done with folder"
}

}
catch
{
write-host $_.exception
}

finally
{
if($sourceListWeb -ne $null){$sourceListWeb.Dispose()}
if($dstListWeb -ne $null){$dstListWeb.Dispose()}
}
  • your script looks really great, but did you wirte something to do the same operation for sharepoint online? – SharePoint TestDev Jul 10 at 21:47
2

You can try this code, CopyItemPermission:

        using (SPSite site = new SPSite("http://yoursite"))
        {
            using (SPWeb web = site.OpenWeb())
            {
                SPList sourceList = web.Lists.TryGetList("SourceLibrary");
                SPList targetList = web.Lists.TryGetList("TargetLibrary");

                SPListItem sourceItem = sourceList.GetItemById(47);
                SPFolder sourceFolder = sourceItem.Folder;
                string targetPath = targetList.RootFolder.ServerRelativeUrl + "/" + sourceFolder.Name;
                sourceFolder.CopyTo(targetPath);

                SPFolder targetFolder = web.GetFolder(targetPath);
                SPListItem targetItem = targetFolder.Item;

                CopyItemPermission(sourceItem, targetItem);
            }
        }

    public static void CopyItemPermission(SPListItem SourceItem, SPListItem TargetItem)
    {
        SPSecurity.RunWithElevatedPrivileges(delegate()
        {
            TargetItem.SystemUpdate();
            if (SourceItem.HasUniqueRoleAssignments)
            {
                TargetItem.BreakRoleInheritance(true);
            }
            else
            {
                return;
                //TargetItem.BreakRoleInheritance(false);
            }

            while (TargetItem.RoleAssignments.Count > 0)
            {
                TargetItem.RoleAssignments.Remove(0);
            }
            //copying all the permission from source item to target item
            foreach (SPRoleAssignment role in SourceItem.RoleAssignments)
            {
                TargetItem.RoleAssignments.Add(role);
            }
            TargetItem.SystemUpdate(false);
        });
    }

Happy Coding !!!

  • Hello Shahjinesh11, How can I use this code to work? I've never used code for SharePoint before.. – Igor Batoukhtine Aug 10 '12 at 12:43
  • Hi Igor, Sorry for late reply. You can create custom webpart or layout page in sharepoint. Also, you can buy kind of product and you can use it if you dont want to create it. example.macroview.com.au/products/pages/… – J.Shah Aug 13 '12 at 11:57
  • is it possible in SharePoint online?? any code is appreciated – santosh kondapalli May 21 at 15:38
  • @sandy143 i have the same case, i need to find a pnp script to move files/folders in SP online.. – SharePoint TestDev Jul 10 at 14:29
  • @J.Shah thanks for the great post ,, but did you have similar script which can work with sharepoint online? – SharePoint TestDev Jul 10 at 21:48
0

I was able to achieve the same in SharePoint online but we need to minimize the code. Please try and let me know your views

How to copy the folder structure and permissions with in the same library

  • has any one tried ... please vote it and let me know if any difficulty – santosh kondapalli Jul 15 at 10:03

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.