Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/sharepoint/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 自动将所有附件下载到SharePoint项目_C#_Sharepoint_Sharepoint 2010 - Fatal编程技术网

C# 自动将所有附件下载到SharePoint项目

C# 自动将所有附件下载到SharePoint项目,c#,sharepoint,sharepoint-2010,C#,Sharepoint,Sharepoint 2010,我有一个控制台应用程序,最终将作为控制台应用程序运行。我需要在外部Sharepoint网站中循环,查找所有新项目,并查看是否有任何附件,如果有,我需要下载这些附件 我能够查询列表,并且能够遍历附件来填充文件集合 FolderToSaveTo解析为“\\APPDEV03\NEMStoPSIMS\”并传递if(System.IO.Directory.Exists(destPath))语句,但在下一行使用(Stream destFile=System.IO.File.OpenWrite(destPat

我有一个控制台应用程序,最终将作为控制台应用程序运行。我需要在外部Sharepoint网站中循环,查找所有新项目,并查看是否有任何附件,如果有,我需要下载这些附件

我能够查询列表,并且能够遍历附件来填充文件集合

FolderToSaveTo解析为“\\APPDEV03\NEMStoPSIMS\”并传递if(System.IO.Directory.Exists(destPath))语句,但在下一行使用(Stream destFile=System.IO.File.OpenWrite(destPath))抛出{“文件名、目录名或卷标语法不正确。\r\n”}

我可以导航到\APPDEV03\NEMStoPSIMS\并且可以手动保存文件

<add key="FolderToSaveTo" value="\\APPDEV03\NEMStoPSIMS\" />

 SharePointConnector.FolderToSaveTo = ConfigurationManager.AppSettings["FolderToSaveTo"];

        try
        {
            ClientContext context = new ClientContext(sp_site_address);

            context.AuthenticationMode = ClientAuthenticationMode.FormsAuthentication;
            context.FormsAuthenticationLoginInfo = new
                                        FormsAuthenticationLoginInfo(username, pwd);

            List list = context.Web.Lists.GetByTitle(requests_list_name);

            CamlQuery query = new CamlQuery();
            query.ViewXml = "<View><RowLimit>100</RowLimit></View>";

            ListItemCollection items = list.GetItems(query);

            context.Load(list);
            context.Load(items);

            context.ExecuteQuery();

            foreach (ListItem oListItem in items)
            {                       

                FileCollection Files = GetAttachments(context, list, oListItem);                    

                foreach (Microsoft.SharePoint.Client.File f in Files)
                {                        
                    Download(f.ServerRelativeUrl, FolderToSaveTo, context);
                }

                lstRequests.Add(Agreement);
            }

        }
        catch (Exception ex)
        {
            throw ex;
        }

    public static FileCollection GetAttachments(ClientContext ctx, List list, ListItem item)
    {            
        ctx.Load(list, l => l.RootFolder.ServerRelativeUrl);
        ctx.Load(ctx.Site, s => s.Url);
        ctx.ExecuteQuery();

        Folder attFolder = ctx.Web.GetFolderByServerRelativeUrl(list.RootFolder.ServerRelativeUrl + "/Attachments/" + item.Id);
        FileCollection files = attFolder.Files;

        ctx.Load(files, fs => fs.Include(f => f.ServerRelativeUrl, f => f.Name, f => f.ServerRelativeUrl));
        ctx.ExecuteQuery();

        return files;

    }

    public static void Download(string serverFilePath, string destPath, ClientContext context)
    {
        using (FileInformation ffl = Microsoft.SharePoint.Client.File.OpenBinaryDirect(context, serverFilePath))
        {
            if (System.IO.Directory.Exists(destPath))
            {
                using (Stream destFile = System.IO.File.OpenWrite(destPath))
                {
                    byte[] buffer = new byte[8 * 1024];
                    int len;
                    while ((len = ffl.Stream.Read(buffer, 0, buffer.Length)) > 0)
                    {
                        destFile.Write(buffer, 0, len);
                    }
                }

            }

        }
    }

SharePointConnector.FolderToSaveTo=ConfigurationManager.AppSettings[“FolderToSaveTo”];
尝试
{
ClientContext=新的ClientContext(sp_站点_地址);
context.AuthenticationMode=ClientAuthenticationMode.FormsAuthentication;
context.FormsAuthenticationLoginInfo=新建
FormsAuthenticationLoginInfo(用户名、密码);
列表=context.Web.Lists.GetByTitle(请求\列表\名称);
CamlQuery query=新建CamlQuery();
query.ViewXml=“100”;
ListItemCollection items=list.GetItems(查询);
加载(列表);
上下文。加载(项目);
context.ExecuteQuery();
foreach(在项目中列出项目列表)
{                       
FileCollection Files=GetAttachments(上下文、列表、oListItem);
foreach(文件中的Microsoft.SharePoint.Client.f文件)
{                        
下载(f.ServerRelativeUrl、FolderToSaveTo、上下文);
}
1.添加(协议);
}
}
捕获(例外情况除外)
{
掷骰子;
}
公共静态文件集合GetAttachments(ClientContext ctx、列表列表、列表项)
{            
Load(list,l=>l.RootFolder.ServerRelativeUrl);
Load(ctx.Site,s=>s.Url);
ctx.ExecuteQuery();
文件夹attFolder=ctx.Web.GetFolderByServerRelativeUrl(list.RootFolder.ServerRelativeUrl+“/Attachments/”+item.Id);
FileCollection files=attFolder.files;
Load(文件,fs=>fs.Include(f=>f.ServerRelativeUrl,f=>f.Name,f=>f.ServerRelativeUrl));
ctx.ExecuteQuery();
归还文件;
}
公共静态无效下载(字符串serverFilePath、字符串destPath、ClientContext上下文)
{
使用(FileInformation ffl=Microsoft.SharePoint.Client.File.OpenBinaryDirect(context,serverFilePath))
{
if(System.IO.Directory.Exists(destPath))
{
使用(Stream destFile=System.IO.File.OpenWrite(destPath))
{
字节[]缓冲区=新字节[8*1024];
内伦;
而((len=ffl.Stream.Read(buffer,0,buffer.Length))>0)
{
destFile.Write(缓冲区,0,len);
}
}
}
}
}

我在没有表单验证的情况下测试了这段代码。我将文件名作为变量传递给下载函数,并完全重写了流到文件部分,并对SharePoint调用进行了一些小的调整。现在效果很好

  static void Main(string[] args)
  {
      using (ClientContext context = new ClientContext(sp_site_address))
      {
            context.AuthenticationMode = ClientAuthenticationMode.FormsAuthentication;
            context.FormsAuthenticationLoginInfo = new FormsAuthenticationLoginInfo(username, pwd);

            List list = context.Web.Lists.GetByTitle(requests_list_name);
            CamlQuery query = new CamlQuery();
            query.ViewXml = "<View><RowLimit>100</RowLimit></View>";
            ListItemCollection items = list.GetItems(query);
            context.Load(items);
            context.ExecuteQuery();

            foreach (ListItem oListItem in items)
            {
                FileCollection files = GetAttachments(context, list, oListItem);
                foreach (Microsoft.SharePoint.Client.File f in files)
                {
                    Download(f.ServerRelativeUrl, FolderToSaveTo, context, f.Name);
                }
                lstRequests.Add(Agreement);
            }
        }
    }

    public static FileCollection GetAttachments(ClientContext ctx, List list, ListItem item)
    {
        ctx.Load(list, l => l.RootFolder.ServerRelativeUrl);
        ctx.Load(ctx.Site, s => s.Url);
        ctx.ExecuteQuery();

        Folder attFolder = ctx.Web.GetFolderByServerRelativeUrl(list.RootFolder.ServerRelativeUrl + "/Attachments/" + item.Id);
        FileCollection files = attFolder.Files;

        ctx.Load(files, fs => fs.Include(f => f.ServerRelativeUrl, f => f.Name, f => f.ServerRelativeUrl));
        ctx.ExecuteQuery();

        return files;

    }

    public static void Download(string serverFilePath, string destPath, ClientContext context, string filename)
    {
        using (FileInformation ffl = Microsoft.SharePoint.Client.File.OpenBinaryDirect(context, serverFilePath))
        {
            if (System.IO.Directory.Exists(destPath))
            {
                using (FileStream fileStream = System.IO.File.Create(destPath + "\\" + filename))
                {
                    using (MemoryStream stream = new MemoryStream())
                    {
                        ffl.Stream.CopyTo(stream);
                        stream.WriteTo(fileStream);
                    }
                }
            }
        }
    }
static void Main(字符串[]args)
{
使用(ClientContext上下文=新的ClientContext(sp_站点_地址))
{
context.AuthenticationMode=ClientAuthenticationMode.FormsAuthentication;
context.FormsAuthenticationLoginInfo=新的FormsAuthenticationLoginInfo(用户名,pwd);
列表=context.Web.Lists.GetByTitle(请求\列表\名称);
CamlQuery query=新建CamlQuery();
query.ViewXml=“100”;
ListItemCollection items=list.GetItems(查询);
上下文。加载(项目);
context.ExecuteQuery();
foreach(在项目中列出项目列表)
{
FileCollection files=GetAttachments(上下文、列表、oListItem);
foreach(文件中的Microsoft.SharePoint.Client.f文件)
{
下载(f.ServerRelativeUrl、FolderToSaveTo、context、f.Name);
}
1.添加(协议);
}
}
}
公共静态文件集合GetAttachments(ClientContext ctx、列表列表、列表项)
{
Load(list,l=>l.RootFolder.ServerRelativeUrl);
Load(ctx.Site,s=>s.Url);
ctx.ExecuteQuery();
文件夹attFolder=ctx.Web.GetFolderByServerRelativeUrl(list.RootFolder.ServerRelativeUrl+“/Attachments/”+item.Id);
FileCollection files=attFolder.files;
Load(文件,fs=>fs.Include(f=>f.ServerRelativeUrl,f=>f.Name,f=>f.ServerRelativeUrl));
ctx.ExecuteQuery();
归还文件;
}
公共静态void下载(字符串serverFilePath、字符串destPath、ClientContext上下文、字符串文件名)
{
使用(FileInformation ffl=Microsoft.SharePoint.Client.File.OpenBinaryDirect(context,serverFilePath))
{
if(System.IO.Directory.Exists(destPath))
{
使用(FileStream FileStream=System.IO.File.Create(destPath+“\\”+文件名))
{
使用(MemoryStream stream=new MemoryStream())
{
ffl.Stream.CopyTo(流);
stream.WriteTo(fileStream);
}
}
}
}
}

我在没有表单验证的情况下测试了这段代码。我将文件名作为一个变量传递给下载函数,并完全