从web api访问sharepoint 2013网站

时间:2018-01-27 18:54:25

标签: c# sharepoint csom

我正在开发用c#和typescript开发的MVC Web应用程序与远程sharepoint站点之间的通信。我想为excel文件执行crud操作。

我能够以这种方式阅读网站属性:

 public async Task<string> getWebTitle(string webUrl, string usr, string psw)
        {
            //Creating Password 
            const string PWD = psw;
            const string USER = usr;
            const string RESTURL = "{0}/_api/web?$select=Title";

            //Creating Credentials 
            var passWord = new SecureString();
            foreach (var c in PWD) passWord.AppendChar(c);
            var credential = new SharePointOnlineCredentials(USER, passWord);


            //Creating Handler to allows the client to use credentials and cookie 
            using (var handler = new HttpClientHandler() { Credentials = credential })
            {
                //Getting authentication cookies 
                Uri uri = new Uri(webUrl);
                handler.CookieContainer.SetCookies(uri, credential.GetAuthenticationCookie(uri));

                //Invoking REST API 
                using (var client = new HttpClient(handler))
                {
                    client.DefaultRequestHeaders.Accept.Clear();
                    client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

                    HttpResponseMessage response = await client.GetAsync(string.Format(RESTURL, webUrl)).ConfigureAwait(false);
                    response.EnsureSuccessStatusCode();

                    string jsonData = await response.Content.ReadAsStringAsync();


                    return jsonData;

                }
            }
        }

jsonData对象返回网站属性。

如何读取保存在“mysite”的Documents文件夹中的文件,例如test.txt?

1 个答案:

答案 0 :(得分:0)

您的意思是想要在SharePoint中获取文件内容吗?

我们可以使用CSOM来实现它。

以下是供您参考的示例:

    public static void getContentOfFileInDocLib(string siteUrl, string host, string sourceListName, string fileName)
    {
        siteUrl = siteUrl.EndsWith("/") ? siteUrl.Substring(0, siteUrl.Length - 1) : siteUrl;
        ClientContext context = new ClientContext(siteUrl);
        Web web = context.Site.RootWeb;

        List source = web.Lists.GetByTitle(sourceListName);

        context.Load(source);           
        context.Load(web);
        context.ExecuteQuery();

        FileCollection files = source.RootFolder.Files;
        Microsoft.SharePoint.Client.File file = files.GetByUrl(siteUrl + "/" + sourceListName + "/" + fileName);
        context.Load(file);
        context.ExecuteQuery();

        FileInformation fileInfo = Microsoft.SharePoint.Client.File.OpenBinaryDirect(context, file.ServerRelativeUrl);
        string filePath = host + file.ServerRelativeUrl;
        System.IO.Stream fileStream = fileInfo.Stream;
        FileCreationInformation createFile = new FileCreationInformation();
        byte[] bufferByte = new byte[1024 * 100];
        System.IO.MemoryStream memory = new System.IO.MemoryStream();
        int len = 0;
        while ((len = fileStream.Read(bufferByte, 0, bufferByte.Length)) > 0)
        {
            memory.Write(bufferByte, 0, len);
        }

        //we get the content of file here
        byte[] bytes = memory.GetBuffer();

        //do something you want
        //.......
    }

将文件上载到SharePoint文档库。

    public static void uploadFile(string siteUrl, string filePath, string fileName, string docLibName)
    {
        siteUrl = siteUrl.EndsWith("/") ? siteUrl.Substring(0, siteUrl.Length - 1) : siteUrl;           
        ClientContext context = new ClientContext(siteUrl);
        List docLib = context.Web.Lists.GetByTitle(docLibName);
        context.Load(docLib);
        context.ExecuteQuery();

        Byte[] bytes = System.IO.File.ReadAllBytes(filePath + fileName);

        FileCreationInformation createFile = new FileCreationInformation();

        createFile.Content = bytes;
        createFile.Url = siteUrl + "/" + docLibName + "/" + fileName;
        createFile.Overwrite = true;
        Microsoft.SharePoint.Client.File newFile = docLib.RootFolder.Files.Add(createFile);
        newFile.ListItemAllFields.Update();
        context.ExecuteQuery();
    }