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

C# CSOM更新库子文件夹中的文件文档元数据。找不到获取错误文件,c#,asp.net,sharepoint-2013,csom,sharepoint-clientobject,C#,Asp.net,Sharepoint 2013,Csom,Sharepoint Clientobject,我有一个URL列表,这些URL是库子文件夹中的文档 当我去更新库中而不是子文件夹中文档的元数据时,代码按预期工作 但列表中的某些文档位于子文件夹中。URL具有包含子文件夹的文档的完全限定路径 /// <summary> /// Updates SharePoint MetaData for a specific document /// </summary> /// <param name="urlfilepath"

我有一个URL列表,这些URL是库子文件夹中的文档

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

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

    /// <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();
        }
    }
我可以将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();
        }
    }
//
///更新特定文档的SharePoint元数据
/// 
///包含要更新的文件的文件名的URL
///这是要更新的propertyName和value的字典。
私有void UpdateMetaData(字符串urlfilepath、字符串库、字典mydictionary)
{
使用(var context=newclientcontext(qsURLSite))
{
context.Credentials=System.Net.CredentialCache.DefaultNetworkCredentials;
var web=context.web;
//获取下拉库并编辑所有项目而不进行更改
Load(web,w=>w.Lists);
context.ExecuteQuery();
List-dropOffLibrary=web.Lists.GetByTitle(库);
Load(dropOffLibrary,dl=>dl.RootFolder.Files);
context.ExecuteQuery();
var file=dropOffLibrary.RootFolder.Files.GetByUrl(urlfilepath);
Load(文件,f=>f.ListItemAllFields);
context.ExecuteQuery();
Microsoft.SharePoint.Client.ListItem newItem=file.ListItemAllFields;
foreach(mydictionary中的KeyValuePair条目)
{
//entry.Key必须位于该文档的属性列表中
//名字很具体
尝试
{
newItem[entry.Key]=entry.Value;
}
抓住
{
//在列表中找不到密钥
//转到下一个键
}
}
newItem.Update();
//file.Update();
加载(文件);
context.ExecuteQuery();
Load(dropOffLibrary,dl=>dl.RootFolder.Files);
context.ExecuteQuery();
}
}

list.RootFolder.Files.GetByUrl
只需返回根文件夹中的文件,您可以使用
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";
            }
        }
//
///此过程将文档上载到授权库
/// 
///正在上载的文件
///以字节为单位的文件
///要上载到的库
/// 
私有字符串UploadToSharePoint(字符串文件名,字节[]p,字符串库)//p是要加载的文件的路径
{
字符串newUrl;
string siteUrl=string.Empty;
siteUrl=qsURL+@“/存储库/当前/”;
//以字节为单位计算块大小。
int blockSize=fileChunkSizeInMB*1024*1024;
//插入凭据
Microsoft.SharePoint.Client.ClientContext上下文=新的Microsoft.SharePoint.Client.ClientContext(siteUrl);
context.Credentials=System.Net.CredentialCache.DefaultNetworkCredentials;
Microsoft.SharePoint.Client.Web站点=context.Web;
if(context.HasPendingRequest)
context.ExecuteQuery();
尝试
{
//获取所需的根文件夹
字符串barRootFolderRelativeUrl=library;
//获取文件夹列表以查看EQCode文件夹是否存在(如果不创建它)。
字符串eq=txtEQCode.Text;
FolderCollection folders=site.GetFolderByServerRelativeUrl(barRootFolderRelativeUrl).folders;//list.RootFolder.folders;
加载(文件夹,fl=>fl.Include(ct=>ct.Name)
其中(ct=>ct.Name==eq));
context.ExecuteQuery();
if(folders.Count<1)//创建文件夹,因为它在SharePoint库中不存在
{
文件夹。添加(eq);
context.ExecuteQuery();
}
Microsoft.SharePoint.Client.Folder barFolder=site.GetFolderByServerRelativeUrl(barRootFolderRelativeUrl);
Microsoft.SharePoint.Client.Folder currentRunFolder=site.GetFolderByServerRelativeUrl(barRootFolderRelativeUrl+“/”+eq);
//Microsoft.SharePoint.Client.FileCreationInformation新建文件=新建Microsoft.SharePoint.Client.FileCreationInformation{Content=p,Url=filename,Overwrite=true};
//Microsoft.SharePoint.Client.File uploadFile=currentRunFolder.Files.Add(新文件);
MemoryStream stream=新的MemoryStream(p);
FileCreationInformation flciNewFile=新的FileCreationInformation();
//这是第一种情况的关键区别——使用ContentStream属性
flciNewFile.ContentStream=流;
flciNewFile.Url=System.IO.Path.GetFileName(fixInvalidCharactersInFileName(文件名));
flciNewFile.Overwrite=false;
Microsoft.SharePoint.Client.File uploadFile=currentRunFolder.Files.Add(flciNewFile);
currentRunFolder.Update();
加载(上传文件);
context.ExecuteQuery();
newUrl=siteUrl+barRootFolderRelativeUrl+“/”+文件名;
//设置文档属性
//必须签出文档以更新属性。
uploadFile.CheckOut();
Microsoft.SharePoint.Cl