从 Web 访问 Sharepoint 2013 网站 api

Access to sharepoint 2013 site from web api

我正在研究用 c# 和打字稿开发的 MVC 网络应用程序与远程共享​​点站点之间的通信。我想对 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 对象 returns 站点属性。

如何读取保存在 "mysite" 的文档文件夹中的文件,例如 test.txt?

您的意思是要获取 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();
    }