Friday, June 5, 2015

Move Folder and Document permissions from once document library to other

Call MainMethod with necessary parameters.
It will copy the folder level/file level broken permissions.


public void MainMethod(string siteCollURL, string sourceRelativeURL, string destinationRelativeURL, string sourceLibName, string destinationLibName)
        {
            try
            {
                using (SPSite site = new SPSite(siteCollURL))
                {
                    using (SPWeb sourceWeb = site.AllWebs[sourceRelativeURL])
                    {
                        using (SPWeb targetWeb = site.AllWebs[destinationRelativeURL])
                        {
                            SPList sourceList = sourceWeb.Lists.TryGetList(sourceLibName);
                            SPList targetList = targetWeb.Lists.TryGetList(destinationLibName);
 
                            if (sourceList != null && targetList != null)
                            {
                                CopySecurity(sourceList, targetList, targetWeb, truefalse);
                                
                            }
  
                        }
                    }
                }
            }
            catch (Exception ex)
            { 
            
            }
        }
 
 
 public static void CopySecurity(SPList sourceList, SPList targetList, SPWeb targetWeb, bool includeItemSecurity, bool quiet)
        {
            if (!quiet)
            {
                LogEntry(string.Format("Start Time: {0}."DateTime.Now.ToString()));
            }
                
 
            try
            {
                if (sourceList.WriteSecurity != targetList.WriteSecurity)
                    targetList.WriteSecurity = sourceList.WriteSecurity;
 
                if (sourceList.ReadSecurity != targetList.ReadSecurity)
                    targetList.ReadSecurity = sourceList.ReadSecurity;
 
                
                SetFolderSecurity(targetWeb, sourceList, targetList, quiet);
                
            }
            catch (Exception ex)
            {
                
            }
            finally
            {
                if (!quiet)
                    LogEntry(string.Format("Finish Time: {0}.\r\n"DateTime.Now.ToString()));
            }
        }
 
 public static void SetFolderSecurity(SPWeb targetWeb, SPList sourceList, SPList targetList, bool quiet)
        {
            foreach (SPListItem sourceFolder in sourceList.Folders)
            {
                SPListItem targetFolder = null;
                foreach (SPListItem f in targetList.Folders)
                {
 
                    if (f.Folder.ServerRelativeUrl.Substring(targetList.RootFolder.ServerRelativeUrl.Length) ==
                        sourceFolder.Folder.ServerRelativeUrl.Substring(sourceList.RootFolder.ServerRelativeUrl.Length))
                    {
                        targetFolder = f;
                        break;
                    }
                }
                if (targetFolder == null)
                    continue;
                CopyRoles(sourceFolder, targetFolder);
                
            }
        }
 
 
private static void CopyRoles(SPListItem source, SPListItem taget)
        {
            if (source.HasUniqueRoleAssignments)
            {
                if (!taget.HasUniqueRoleAssignments)
                    taget.BreakRoleInheritance(false);
 
                for (int i = taget.RoleAssignments.Count - 1; i >= 0; i--)
                    taget.RoleAssignments.Remove(i);
 
                foreach (SPRoleAssignment role in source.RoleAssignments)
                {
                    //if role contains only "Restricted access" permission level
                    if (role.RoleDefinitionBindings.Count == 1 &&
                        role.RoleDefinitionBindings[0].Type == SPRoleType.Guest)
                    {
                        taget.RoleAssignments.Add(role.Member);
                    }
                    else
                        taget.RoleAssignments.Add(role);
                }
 
                taget.SystemUpdate(false);
            }
 
            // check items in the folder
            foreach (SPFile sourceFile in source.Folder.Files)
            {
                if (sourceFile.Item.HasUniqueRoleAssignments)
                {
                    foreach (SPFile destFile in taget.Folder.Files)
                    {
                        if (sourceFile.Name == destFile.Name)
                        {
                            // do copy permissions
                            CopyRoles2(sourceFile.Item, destFile.Item);
                            break;
                        }
                    }
                }
                
            }
        } 

private static void CopyRoles2(SPListItem source, SPListItem taget)
        {
            if (source.HasUniqueRoleAssignments)
            {
                if (!taget.HasUniqueRoleAssignments)
                    taget.BreakRoleInheritance(false);
 
                for (int i = taget.RoleAssignments.Count - 1; i >= 0; i--)
                    taget.RoleAssignments.Remove(i);
 
                foreach (SPRoleAssignment role in source.RoleAssignments)
                {
                    //if role contains only "Restricted access" permission level
                    if (role.RoleDefinitionBindings.Count == 1 &&
                        role.RoleDefinitionBindings[0].Type == SPRoleType.Guest)
                    {
                        taget.RoleAssignments.Add(role.Member);
                    }
                    else
                        taget.RoleAssignments.Add(role);
                }
 
                taget.SystemUpdate(false);
            }
        }

 
 

No comments:

Post a Comment

Image noise comparison methods

 1. using reference image technique     - peak_signal_noise_ratio (PSNR)     - SSI 2. non-reference image technique     - BRISQUE python pac...