简体   繁体   English

通过Web API访问Sharepoint 2013网站

[英]Access to sharepoint 2013 site from web api

I'm working on a communication between a MVC web app developed in c# and typescript and a remote sharepoint site. 我正在使用C#和Typescript开发的MVC Web应用程序与远程共享​​点站点之间进行通信。 I want to execute crud operations for an excel file. 我想对Excel文件执行Crud操作。

I'm able to read site properties in this way: 我可以通过以下方式读取网站属性:

 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 object returns site properties. jsonData对象返回站点属性。

How I can do to read a file, for example test.txt, saved in Documents folder in "mysite"? 如何读取保存在“ mysite”中“文档”文件夹中的文件(例如test.txt)?

Do you mean that you want to get the content of file in SharePoint? 您是否要在SharePoint中获取文件的内容?

We can use CSOM to achieve it. 我们可以使用CSOM实现它。

Here is a sample for your reference: 这是一个示例供您参考:

    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
        //.......
    }

Upload file to SharePoint Document Library. 将文件上传到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();
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从SharePoint网站访问文件 - Access a file from a SharePoint site 如何通过无缝身份验证从 microsoft word 2013 插件在 office 365 sharepoint 2013 站点中保存文档? - How to save document in office 365 sharepoint 2013 site from microsoft word 2013 addin with seamless authentication? MVC .NET Web服务在部署模式下无法访问Sharepoint网站 - MVC .NET Web service no access to Sharepoint Site on deploy mode Sharepoint 2013服务器对象模型。 在网站创建期间将权限从一个网站复制到另一个网站 - Sharepoint 2013 Server Object Model. Copy permissions from one site to another during site creation 我正在尝试从 Web API 应用程序(在 IIS 上发布)访问数据到控制台应用程序(VSExpress2013) - I am trying to access data from web API application(pulished at IIS) to console applicatication(VSExpress2013) 验证站点/应用程序以访问Web API服务 - Authenticate a site/app to access a Web API Service sharepoint 2013结果源api - sharepoint 2013 result source api 在VS2013中从网站访问本地文件 - Accessing Local file from Web Site in VS2013 在没有Sharepoint环境的情况下,使用来自.Net的Rest API,为Sharepoint 2013中的用户设置文件夹权限? - Set folder permission for user in Sharepoint 2013 using Rest API from .Net without Sharepoint environment? 从Windows窗体应用程序(Web服务)登录到Sharepoint网站 - Login to Sharepoint site from Windows Form Application (Web Services)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM