CSOM 更新库子文件夹中的文件文档元数据。找不到错误文件

CSOM Update File Document MetaData in a Subfolder of a library. Getting Error File Not Found

我有一个 URL 列表,它们是图书馆子文件夹中的文档。

当我去更新库中但不在子文件夹中的文档的元数据时,代码按预期工作。

但列表中的某些文档位于子文件夹中。 URL 具有包含子文件夹的文档的完全限定路径。

我可以将 url 粘贴到浏览器中并打开文档,这样我就知道它是正确的,但是当我到达

context.Load(file, f.ListItemsAllFields); 
Context.ExecuteQuery();  -- Error Happens on URL of file in subfolder  

我收到一条错误消息,提示“找不到文件”。

查看代码...如果文件位于库的根目录中,但当文件位于子文件夹中时则无效。

    /// <summary>
    /// Updates SharePoint MetaData for a specific document
    /// </summary>
    /// <param name="urlfilepath">The URL including file name of the file to be updated</param>
    /// <param name="values">this is a dictionary of propertyName and value to be updated.</param>
    private void UpdateMetaData(string urlfilepath, string library,Dictionary<string, string> mydictionary)
    {
        using (var context = new ClientContext(qsURLSite))
        {
            context.Credentials = System.Net.CredentialCache.DefaultNetworkCredentials;
            var web = context.Web;

            // Get drop off library and edit all items without making changes
            context.Load(web, w => w.Lists);
            context.ExecuteQuery();

            List dropOffLibrary = web.Lists.GetByTitle(library);
            
            context.Load(dropOffLibrary, dl => dl.RootFolder.Files);
            context.ExecuteQuery();

            var file = dropOffLibrary.RootFolder.Files.GetByUrl(urlfilepath);
         
            context.Load(file, f => f.ListItemAllFields);
            context.ExecuteQuery();

            Microsoft.SharePoint.Client.ListItem newItem = file.ListItemAllFields;


            foreach (KeyValuePair<string, string> entry in mydictionary)
            {
                // entry.Key has to be in the property list of that document
                // the name is very specific
                try
                {
                    newItem[entry.Key] = entry.Value;
                }
                catch
                {
                    // Key was not found in list
                    // go to next key
                }
            }
            newItem.Update();
            //file.Update();
            context.Load(file);
            context.ExecuteQuery();

            context.Load(dropOffLibrary, dl => dl.RootFolder.Files);
            context.ExecuteQuery();
        }
    }

list.RootFolder.Files.GetByUrl 只是根文件夹中的 return 文件,您可以使用 web.GetFileByServerRelativeUrl 代替。

Web web = context.Web;
                var list = web.Lists.GetByTitle("MyDoc3");
                var file1 =web.GetFileByServerRelativeUrl("/sites/lee/MyDoc3/ParentFolder/test2.docx");                
                var file2 = list.RootFolder.Files.GetByUrl("/sites/lee/MyDoc3/test.pptx");
                context.Load(file1);
                context.Load(file2);
                context.ExecuteQuery();

这是我最终开始工作的结果。

eq 是我代码中下面 EQCode 的记录 ID 缩写。

/// <summary>
      /// This procedure uploads documents to the Authorizations Library
      /// </summary>
      /// <param name="filename">File Being Uploaded</param>
      /// <param name="p">file in bytes</param>
      /// <param name="library">Library to upload to</param>
      /// <returns></returns>
        private string UploadToSharePoint(string filename, byte[] p,string library)  //p is path to file to load
        {
            string newUrl;
            string siteUrl = string.Empty; 


            siteUrl = qsURL + @"/repository/current/";
            // Calculate block size in bytes.
            int blockSize = fileChunkSizeInMB * 1024 * 1024;

            //Insert Credentials

             Microsoft.SharePoint.Client.ClientContext context = new Microsoft.SharePoint.Client.ClientContext(siteUrl);

            context.Credentials = System.Net.CredentialCache.DefaultNetworkCredentials;
            Microsoft.SharePoint.Client.Web site = context.Web;

            if (context.HasPendingRequest)
                context.ExecuteQuery();

            try
            {

                //Get the required RootFolder
                string barRootFolderRelativeUrl = library;

                // Getting Folder List to see if EQCode Folder Exists  If not create it.

                string eq = txtEQCode.Text;
                FolderCollection folders = site.GetFolderByServerRelativeUrl(barRootFolderRelativeUrl).Folders;   //list.RootFolder.Folders;
                context.Load(folders, fl => fl.Include(ct => ct.Name)
                .Where(ct => ct.Name == eq));
                context.ExecuteQuery();

                if (folders.Count < 1)  // Create Folder because it does not exist on SharePoint Library
                {
                    folders.Add(eq);
                    context.ExecuteQuery();
                }

                Microsoft.SharePoint.Client.Folder barFolder = site.GetFolderByServerRelativeUrl(barRootFolderRelativeUrl);
                Microsoft.SharePoint.Client.Folder currentRunFolder = site.GetFolderByServerRelativeUrl(barRootFolderRelativeUrl + "/" + eq);

                //Microsoft.SharePoint.Client.FileCreationInformation newFile = new Microsoft.SharePoint.Client.FileCreationInformation { Content = p, Url = filename, Overwrite = true };
                //Microsoft.SharePoint.Client.File uploadFile = currentRunFolder.Files.Add(newFile);

                MemoryStream stream = new MemoryStream(p);

                FileCreationInformation flciNewFile = new FileCreationInformation();

                // This is the key difference for the first case – using ContentStream property
                flciNewFile.ContentStream = stream;
                flciNewFile.Url = System.IO.Path.GetFileName(fixInvalidCharactersInFileName(filename));
                flciNewFile.Overwrite = false;
                Microsoft.SharePoint.Client.File uploadFile = currentRunFolder.Files.Add(flciNewFile);

                currentRunFolder.Update();
                context.Load(uploadFile);
                context.ExecuteQuery();

                newUrl = siteUrl + barRootFolderRelativeUrl + "/" + filename;

                // Set document properties
                // Have to check out document to upldate properties.

                uploadFile.CheckOut();

                Microsoft.SharePoint.Client.ListItem listItem = uploadFile.ListItemAllFields;

                // Sets up ListItem fields based on Content Type 
                // as each content type has a different set of Required Fields.

                SetUpListItems(listItem, EQCode);

                listItem["Title"] = filename;
                listItem.Update();

                uploadFile.CheckIn("File Uploaded Manually added metadata", Microsoft.SharePoint.Client.CheckinType.MinorCheckIn);
                context.ExecuteQuery();

                //Return the URL of the new uploaded file
                return newUrl;
            }
            catch (Exception e)
            {
                Page.ClientScript.RegisterStartupScript(this.GetType(), "Error Uploading!", "alert('" + e.Message + "');", true);
                return "FALSE";
            }
        }