SharePoint在线(O365):如何获取父项URL?

时间:2017-06-28 06:53:35

标签: sharepoint csom o365rwsclient

拥有以下网址

https://test.sharepoint.com/shared%20documents/MyFolder1/myImg1.jpeg

我需要发送此项目的父网址请求(在此特定情况下为图片)并获得回复:

https://test.sharepoint.com/shared%20documents/MyFolder1/

如果使用CSOM或Office 365活动API,是否存在此类请求? 我可以在文档中找到这样的例子吗?

1 个答案:

答案 0 :(得分:0)

static public string ReturnFileParentUrl(string url)
        {
            try
            {
                Uri uri = new Uri("https://XXX.sharepoint.com/");
                using (var ctx = GetContext(uri, "YYY@DOMAIN.com", "password"))
                {

                    Microsoft.SharePoint.Client.File item = ctx.Web.GetFileByServerRelativeUrl(url);

                    ctx.Load(item);
                    ctx.ExecuteQuery();

                    Console.WriteLine("file: {0}\n", item.Name);
                    Console.WriteLine("Type: {0}\n", item.TypedObject.ToString());
                    if (item.TypedObject.ToString() == "Microsoft.SharePoint.Client.File") //To check if there is a better way to check if the item is a file 
                        if (item.ServerObjectIsNull != true)
                        {
                            ctx.Load(item, i => i.ListItemAllFields);
                            ctx.ExecuteQuery();
                            var folder = ctx.Web.GetFolderByServerRelativeUrl((string)item.ListItemAllFields["FileDirRef"]);
                            ctx.Load(folder);
                            ctx.ExecuteQuery();
                            Console.WriteLine("parent relative url {0}\n", folder.ServerRelativeUrl);
                            return folder.ServerRelativeUrl;
                        }

                    return null;
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            return null;
        }