-1

I need to copy a particular file from one library to another library.

At first, need to check if file is existing in that library.

If Existing, then need to overwrite file content and new sharepoint version should be updated for that document.

I need to do this using c# CSOM and sharepoint version is 2013.

Thanks in advance :)

2 Answers2

0
public static void CopyDocuments(string srcUrl, string destUrl, string srcLibrary, string destLibrary, Login _login)
{
    // set up the src client
    SP.ClientContext srcContext = new SP.ClientContext(srcUrl);
    srcContext.AuthenticationMode = SP.ClientAuthenticationMode.FormsAuthentication;
    srcContext.FormsAuthenticationLoginInfo = new SP.FormsAuthenticationLoginInfo(_login.UserName, _login.Password);

    // set up the destination context (in your case there is no needs to create a new context, because it would be the same library!!!!)
    SP.ClientContext destContext = new SP.ClientContext(destUrl);
    destContext.AuthenticationMode = SP.ClientAuthenticationMode.FormsAuthentication;
    destContext.FormsAuthenticationLoginInfo = new SP.FormsAuthenticationLoginInfo(_login.UserName, _login.Password);

    // get the list and items
    SP.Web srcWeb = srcContext.Web;
    SP.List srcList = srcWeb.Lists.GetByTitle(srcLibrary);
    SP.ListItemCollection col = srcList.GetItems(new SP.CamlQuery());
    srcContext.Load(col);
    srcContext.ExecuteQuery();

    // get the new list
    SP.Web destWeb = destContext.Web;
    destContext.Load(destWeb);
    destContext.ExecuteQuery();

    foreach (var doc in col)
    {
        try
        {
            if (doc.FileSystemObjectType == SP.FileSystemObjectType.File)
            {
                // get the file
                SP.File f = doc.File;
                srcContext.Load(f);
                srcContext.ExecuteQuery();

                // build new location url
                string nLocation = destWeb.ServerRelativeUrl.TrimEnd('/') + "/" + destLibrary.Replace(" ", "") + "/" + f.Name;

                // read the file, copy the content to new file at new location
                SP.FileInformation fileInfo = SP.File.OpenBinaryDirect(srcContext, f.ServerRelativeUrl);
                SP.File.SaveBinaryDirect(destContext, nLocation, fileInfo.Stream, true);
            } 

            if (doc.FileSystemObjectType == SP.FileSystemObjectType.Folder)
            {
                // load the folder
                srcContext.Load(doc);
                srcContext.ExecuteQuery();

                // get the folder data, get the file collection in the folder
                SP.Folder folder = srcWeb.GetFolderByServerRelativeUrl(doc.FieldValues["FileRef"].ToString());
                SP.FileCollection fileCol = folder.Files;

                // load everyting so we can access it
                srcContext.Load(folder);
                srcContext.Load(fileCol);
                srcContext.ExecuteQuery();

                foreach (SP.File f in fileCol)
                {
                    // load the file
                    srcContext.Load(f);
                    srcContext.ExecuteQuery();

                    string[] parts = null;
                    string id = null;

                    if (srcLibrary == "My Files")
                    {
                        // these are doc sets
                        parts = f.ServerRelativeUrl.Split('/');
                        id = parts[parts.Length - 2];
                    }
                    else
                    {
                        id = folder.Name;
                    }

                    // build new location url
                    string nLocation = destWeb.ServerRelativeUrl.TrimEnd('/') + "/" + destLibrary.Replace(" ", "") + "/" + id + "/" + f.Name;

                    // read the file, copy the content to new file at new location
                    SP.FileInformation fileInfo = SP.File.OpenBinaryDirect(srcContext, f.ServerRelativeUrl);
                    SP.File.SaveBinaryDirect(destContext, nLocation, fileInfo.Stream, true);
                }
            }
        }
        catch (Exception ex)
        {
            Log("File Error = " + ex.ToString());
        }
    }
}

Source: https://sharepoint.stackexchange.com/questions/114033/how-do-i-move-files-from-one-document-library-to-another-using-jsom

Community
  • 1
  • 1
Nikerym
  • 515
  • 3
  • 12
0

I strongly advise against using the approach suggested by Nikerym. You don't want to download the bytes only to upload them unmodified. It's slow and error-prone. Instead, use the built-in method provided by the CSOM API.

https://learn.microsoft.com/en-us/previous-versions/office/sharepoint-server/mt162553(v=office.15)?redirectedfrom=MSDN

var srcPath = "https://YOUR.sharepoint.com/sites/xxx/SitePages/Page.aspx";
var destPath = $"https://YOUR.sharepoint.com/sites/xxx/SitePages/CopiedPage.aspx";
                  
MoveCopyUtil.CopyFileByPath(ctx, ResourcePath.FromDecodedUrl(srcPath), ResourcePath.FromDecodedUrl(destPath), false, new MoveCopyOptions());
ctx.ExecuteQuery();

You can configure the override behavior by adjusting the 4th and 5th arguments of the function signature.

[...]
bool overwrite,
MoveCopyOptions options

https://learn.microsoft.com/en-us/previous-versions/office/sharepoint-server/mt844930(v=office.15)

Yannic Hamann
  • 4,655
  • 32
  • 50