Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

I want to copy a folder from on 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 got an awnser to this????

share|improve this question

2 Answers 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 !!!

share|improve this answer
    
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

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()}
}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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