TARUN February 2016

Creating Folders dynamically using CSOM in sharepoint causes ID issues for the document

I am creating the nested folders dynamically for a document library and uploading files. The below code is working fine. but the issues is whenever i uploading the file to the existence folder the ID of the document for the uploaded documents is not in sequence. Suppose if i am uploading a file to path projects\PRJ1\Assignment, fist time it was creating the folders and file with ID's(1\2\3\4) respectively . If i upload other document to the same folder the id of the folders is not changing but the id of the file is 8. i am not unable to find the id 5,6,7 in the document library.

    using (CSOM.ClientContext clientContext = new CSOM.ClientContext(SPserverUrl))
                {
      clientContext.Credentials = new System.Net.NetworkCredential(Username, Password, Domain);
                    CSOM.Web _Site = clientContext.Web;
                   // string listrootfolder = "Testlist";
                    CSOM.List _List = _Site.Lists.GetByTitle("Testlist");
                    clientContext.Load(_List.RootFolder);
                    clientContext.ExecuteQuery();
                    string listrootfolder = _List.RootFolder.Name.ToString();
    var folder = CreateFolder(clientContext.Web, "Testlist", folderpath);

                        // uploading the document
                        CSOM.FileCreationInformation newFile = new CSOM.FileCreationInformation();
                        // newFile.Content = System.IO.File.ReadAllBytes(@"D:\Testupload.docx");  
                        byte[] bytes = Convert.FromBase64String(objReqDocumentDetials.FileData.ToString());
                        newFile.Content = bytes;
                        //   newFile.Url = objDocumentDetials.AttachmentName.ToString() + DateTime.Now.ToString("ddMMyyyyHHmmsss") + "." +          objDocumentDetials.FileType.ToString();
                        newFile.Url = objReqDocumentDetials.FileName.ToString() + "." + objReqDocumentDetials.FileType.ToString();
                              

Answers


TARUN February 2016

The issue was fixed after redefined the CreateFolder and CreateFolderInternal methods as below

    public CSOM.Folder CreateFolder(CSOM.Web web, string listTitle, string fullFolderPath)
    {

        var folderUrls = fullFolderPath.Split(new char[] { '/' }, StringSplitOptions.RemoveEmptyEntries);

        CSOM.List _List = web.Lists.GetByTitle(listTitle);
        web.Context.Load(_List.RootFolder);
        web.Context.ExecuteQuery();
        string listrootfolder = _List.RootFolder.Name.ToString();
        web.Context.Load(web, w => w.ServerRelativeUrl);
        web.Context.ExecuteQuery();
        string root = "";
        for (int i = 0; i < folderUrls.Length; i++)
        {
            root = folderUrls[i].ToString();
            string targetFolderUrl = "/" + listrootfolder + "/" + string.Join("/", folderUrls, 0, i + 1);
            var folder1 = web.GetFolderByServerRelativeUrl(targetFolderUrl);
            web.Context.Load(folder1);
            bool exists = false;
            try
            {
                web.Context.ExecuteQuery();
                exists = true;
            }
            catch (Exception ex)
            {
            }
            if (!exists)
            {
                if (i == 0)
                {
                    CreateFolderInternal(web, _List.RootFolder, root);
                }
                else
                {
                    web.Context.Load(web, w => w.ServerRelativeUrl);
                    web.Context.ExecuteQuery();
                    var targetfolderUrls = targetFolderUrl.Split(new char[] { '/' }, StringSplitOptions.RemoveEmptyEntries);
                    string jj = string.Join("/", targetfolderUrls, 0, targetfolderUrls.Length - 1);
                    CSOM.Folder folder = web.GetFolderByServerRelativeUrl(web.ServerRelativeUrl + jj);
                    web.Context.Load(folder);
                    web.Context.ExecuteQuery();
                    SPCreateFolderInternal(web, folder 

Post Status

Asked in February 2016
Viewed 2,442 times
Voted 11
Answered 1 times

Search




Leave an answer