0

I have a console application in which I am trying to move files from one folder to another folder in same document library using CSOM. While doing so I am getting below error:

(400) Bad Request

I have taken reference from this post

My code is as below:

using System;
using System.IO;
using System.Linq;
using System.Security;
using Microsoft.SharePoint.Client;
using ClientOM = Microsoft.SharePoint.Client;

namespace ConsoleApp2
{
    public static class FolderExtensions
    {
        public static void MoveFilesTo(this Folder folder, string folderUrl)
        {
            var ctx = (ClientContext)folder.Context;

        //SecureString passWord = new SecureString();
        //foreach (char c in "HelloWorld@1234".ToCharArray()) passWord.AppendChar(c);
        //ctx.Credentials = new SharePointOnlineCredentials("jay.desai@companyname.com", passWord);

        if (!ctx.Web.IsPropertyAvailable("https://company.sharepoint.com"))
        {
            ctx.Load(ctx.Web, w => w.ServerRelativeUrl);
        }
        ctx.Load(folder, f => f.Files, f => f.ServerRelativeUrl, f => f.Folders);
        ctx.ExecuteQuery();

        //Ensure target folder exists
        //ctx.Web.EnsureFolder(folderUrl.Replace(ctx.Web.ServerRelativeUrl, string.Empty));
        foreach (var file in folder.Files)
        {
            var targetFileUrl = file.ServerRelativeUrl.Replace(folder.ServerRelativeUrl, folderUrl);
            file.MoveTo(targetFileUrl, MoveOperations.Overwrite);
        }
        ctx.ExecuteQuery();

       }
    }

//static class WebExtensions
//{
//    /// <summary>
//    /// Ensures whether the folder exists   
//    /// </summary>
//    /// <param name="web"></param>
//    /// <param name="folderUrl"></param>
//    /// <returns></returns>
//    public static Folder EnsureFolder(this Web web, string folderUrl)
//    {
//        return EnsureFolderInternal(web.RootFolder, folderUrl);
//    }


//    private static Folder EnsureFolderInternal(Folder parentFolder, string folderUrl)
//    {
//        var ctx = parentFolder.Context;
//        var folderNames = folderUrl.Split(new char[] { '/' }, StringSplitOptions.RemoveEmptyEntries);
//        var folderName = folderNames[0];
//        var folder = parentFolder.Folders.Add(folderName);
//        ctx.Load(folder);
//        ctx.ExecuteQuery();

//        if (folderNames.Length > 1)
//        {
//            var subFolderUrl = string.Join("/", folderNames, 1, folderNames.Length - 1);
//            return EnsureFolderInternal(folder, subFolderUrl);
//        }
//        return folder;
//    }
//}

 class Program
 {

    static void Main(string[] args)
    {
        var srcFolderUrl = "/sites/DataServices/Shared Documents/Data Dictionaries";
        var destFolderUrl = "/sites/DataServices/Shared Documents/Data Dictionaries/Archive";
        using (var ctx = new ClientContext("https://companyname.sharepoint.com"))
        {
            var sourceFolder = ctx.Web.GetFolderByServerRelativeUrl(srcFolderUrl);
            sourceFolder.MoveFilesTo(destFolderUrl);
            ctx.ExecuteQuery();
        }
    }  
  }
}

I am getting error at ctx.ExecuteQuery(); enter image description here

0

@Jay Desai,

Can you point which line prompts this error?

And i think below code should be:

if (!ctx.Web.IsPropertyAvailable("https://company.sharepoint.com"))

==> if (!ctx.Web.IsPropertyAvailable("ServerRelativeUrl"))

Besides, when you generate a client context, the site URL should be the site where the targeted folder located at.

Below is my test demo, it works on My SPO library.

using Microsoft.SharePoint.Client;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security;
using System.Text;
using System.Threading.Tasks;

namespace TeamifySharePointClassicSite.moveFolder
{
    class FolderExtensionsapp
    {
        static void Main(string[] args)
        {
            var srcFolderUrl = "/sites/testprivate/mytestdoclib/Data Dictionaries";
            var destFolderUrl = "/sites/testprivate/mytestdoclib/Data Dictionaries/Archive";

            var password = "xxxxxxxxx";
            var username = "admin@abc.onmicrosoft.com";
            string SiteUrl = "https://abc.sharepoint.com/sites/testprivate";
            SecureString securestring = new SecureString();
            password.ToCharArray().ToList().ForEach(s => securestring.AppendChar(s));        
            
            using (var ctx = new ClientContext(SiteUrl))
            {
                ctx.Credentials= new SharePointOnlineCredentials(username, securestring);
                var sourceFolder = ctx.Web.GetFolderByServerRelativeUrl(srcFolderUrl);
                sourceFolder.MoveFilesTo(destFolderUrl);
                ctx.ExecuteQuery();
            }

        }
    }

    public static class FolderExtensions
    {
        public static void MoveFilesTo(this Folder folder, string folderUrl)
        {
            var ctx = (ClientContext)folder.Context;
            if (!ctx.Web.IsPropertyAvailable("ServerRelativeUrl"))
            {
                ctx.Load(ctx.Web, w => w.ServerRelativeUrl);
            }
            ctx.Load(folder, f => f.Files, f => f.ServerRelativeUrl, f => f.Folders);
            ctx.ExecuteQuery();

            //Ensure target folder exists
            ctx.Web.EnsureFolder(folderUrl.Replace(ctx.Web.ServerRelativeUrl, string.Empty));
            foreach (var file in folder.Files)
            {
                var targetFileUrl = file.ServerRelativeUrl.Replace(folder.ServerRelativeUrl, folderUrl);
                file.MoveTo(targetFileUrl, MoveOperations.Overwrite);
            }
            ctx.ExecuteQuery();

            //foreach (var subFolder in folder.Folders)
            //{
            //    var targetFolderUrl = subFolder.ServerRelativeUrl.Replace(folder.ServerRelativeUrl, folderUrl);
            //    subFolder.MoveFilesTo(targetFolderUrl);
            //}
        }

    }

    static class WebExtensions
    {
        /// <summary>
        /// Ensures whether the folder exists   
        /// </summary>
        /// <param name="web"></param>
        /// <param name="folderUrl"></param>
        /// <returns></returns>
        public static Folder EnsureFolder(this Web web, string folderUrl)
        {
            return EnsureFolderInternal(web.RootFolder, folderUrl);
        }


        private static Folder EnsureFolderInternal(Folder parentFolder, string folderUrl)
        {
            var ctx = parentFolder.Context;
            var folderNames = folderUrl.Split(new char[] { '/' }, StringSplitOptions.RemoveEmptyEntries);
            var folderName = folderNames[0];
            var folder = parentFolder.Folders.Add(folderName);
            ctx.Load(folder);
            ctx.ExecuteQuery();

            if (folderNames.Length > 1)
            {
                var subFolderUrl = string.Join("/", folderNames, 1, folderNames.Length - 1);
                return EnsureFolderInternal(folder, subFolderUrl);
            }
            return folder;
        }
    }
}

BR

1
  • I'm still getting same error at same line. I have updated my question with error screenshot.
    – Jay Desai
    Oct 9 '20 at 14:04
0

Problem with the configuration of console application. I was using .NET core. Finally when I created console application with .NET Framework it worked.

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.