简体   繁体   中英

Getting files/folders from sharepoint via the api in c#

I'm trying to link ac# application to a sharepoint directory, so I can create folders, download and upload files. However I am strugling with connecting to the correct folder.

I can retrieve the content from allitems.aspx, but I am not sure how to actually get the content from folder.

I have tried using the ClientContext - something like this:

        ClientContext cxt = new ClientContext("https://xx.sharepoint.com/sites/");

        cxt.Credentials = GetCredentials();

        List list = cxt.Web.Lists.GetByTitle("Kontrakter");
        var test = list.Views;
        var test1 = cxt.Web.Lists;

        cxt.Load(test1);
        cxt.Load(list);
        cxt.Load(test);
        var a = 4;

        var fullUri = new Uri("https://xx.sharepoint.com/sites/yy/Kontrakter/AllItems.aspx");
        //var folder = cxt.Web.GetFolderByServerRelativeUrl(fullUri.AbsolutePath);

        using (var rootCtx = new ClientContext(fullUri.GetLeftPart(UriPartial.Authority))) 
        {
            rootCtx.Credentials = GetCredentials();
            Uri webUri = Web.WebUrlFromPageUrlDirect(rootCtx, fullUri);  
            using (var ctx1 = new ClientContext(webUri))
            {
                ctx1.Credentials = GetCredentials();

                var list1 = ctx1.Web.GetList(fullUri.AbsolutePath); 
                ctx1.Load(list1.RootFolder.Files);
                ctx1.ExecuteQuery();

                Console.WriteLine(list.RootFolder.Files.Count);


            }
        }

or via normal api calls like this:

https://xx.sharepoint.com/_api/Web/GetFolderByServerRelativeUrl ('Kontrakter/Forms')/Files

The only way I can find some data is if I look into 'Shared documents/Forms'

I'm having problems understanding the directory structure and how I can actually find the content of files/folders.

Thanks in advance :)

原来我在我的一个 uri 中缺少一个 /sites。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM