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


i creating nested folders dynamically document library , uploading files. below code working fine. issues whenever uploading file existence folder id of document uploaded documents not in sequence. suppose if uploading file path projects\prj1\assignment, fist time creating folders , file id's(1\2\3\4) respectively . if upload other document same folder id of folders not changing id of file 8. not unable find id 5,6,7 in 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 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();                         newfile.overwrite = true;                         microsoft.sharepoint.client.file _uploadingfile = folder.files.add(newfile);                          var item = _uploadingfile.listitemallfields;                         // var folder = item.getfolder("account/" + foldername);                       //  item["year"] = datetime.now.year.tostring();                     //    item.update();                          clientcontext.load(_uploadingfile, f => f.listitemallfields);                         clientcontext.executequery();     }      public csom.folder createfolder(csom.web web, string listtitle, string fullfolderpath)     {         if (string.isnullorempty(fullfolderpath))             throw new argumentnullexception("fullfolderpath");         csom.list list = web.lists.getbytitle(listtitle);         return createfolderinternal(web, list.rootfolder, fullfolderpath);     }      public csom.folder createfolderinternal(csom.web web, csom.folder parentfolder, string fullfolderpath)     {         var folderurls = fullfolderpath.split(new char[] { '/' }, stringsplitoptions.removeemptyentries);         string folderurl = folderurls[0];         var curfolder = parentfolder.folders.add(folderurl);         web.context.load(curfolder);         web.context.executequery();          if (folderurls.length > 1)         {             var folderpath = string.join("/", folderurls, 1, folderurls.length - 1);             return createfolderinternal(web, curfolder, folderpath);         }         return curfolder;     } 

the issue fixed after redefined createfolder , createfolderinternal methods 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 = "";         (int = 0; < folderurls.length; i++)         {             root = folderurls[i].tostring();             string targetfolderurl = "/" + listrootfolder + "/" + string.join("/", folderurls, 0, + 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, root);                 }             }             else             {                 //already folder exists             }         }         csom.folder finalfolder = web.getfolderbyserverrelativeurl(web.serverrelativeurl + listrootfolder + "/" + fullfolderpath);         web.context.load(finalfolder);         web.context.executequery();         return finalfolder;     }     private void createfolderinternal(csom.web web, csom.folder parentfolder, string fullfolderpath)     {         try         {             var curfolder = parentfolder.folders.add(fullfolderpath);             web.context.load(curfolder);             web.context.executequery();         }         catch (system.exception ex)         {                        }     } 

Comments

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -