C# 如何从 Windows 应用程序中检索用户有权访问的共享点站点列表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1205050/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 10:51:21  来源:igfitidea点击:

How to retrieve the list of sharepoint sites, user has access to , from a windows application

c#sharepoint

提问by Kapil

I need to retrieve a list of all the SharePoint sites to which I have access to via a windows application (C#). I am planning to use SharePoint web services.

我需要检索我可以通过 Windows 应用程序 (C#) 访问的所有 SharePoint 站点的列表。我计划使用 SharePoint Web 服务。

Any pointers using SharePoint web services which could provide me the required information?

任何使用 SharePoint Web 服务的指针可以为我提供所需的信息?

回答by Colin

There's no way to do this with the out of the box web services I think. You could however write your web service, deploy that to your sharepoint farm and then call that service.

我认为开箱即用的 Web 服务无法做到这一点。但是,您可以编写您的 Web 服务,将其部署到您的共享点场,然后调用该服务。

In it should be a method that takes a username and then using SPSecurity.RunWithElevatedPriviliges loops through the site collections / sites to determine if the user provided has access to each.

其中应该是一种方法,它采用用户名,然后使用 SPSecurity.RunWithElevatedPriviliges 循环访问网站集/网站,以确定提供的用户是否有权访问每个网站集。

回答by Rob Windsor

Webs.asmx should do the trick. Here's a snippet to get you started.

Webs.asmx 应该可以解决问题。这是一个可以帮助您入门的片段。

Dim rootNode As XmlNode = Nothing

Using ws As New WebsProxy.Webs
    ws.PreAuthenticate = True
    ws.UseDefaultCredentials = True
    ws.Url = <site collection address> + "/_vti_bin/webs.asmx"
    rootNode = ws.GetWebCollection()
End Using

回答by Michael Ciba

If you want to use the API instead then I would suggest you do the following to return all the sub webs for the current user without having to use elevated priviliges.

如果您想改用 API,那么我建议您执行以下操作以返回当前用户的所有子网站,而无需使用提升的权限。

using(SPSite site = new SPSite("http://example/site/"))
{
    using (SPWeb web = site.OpenWeb())
    {
        SPWebCollection webCollection = web.GetSubwebsForCurrentUser();
    }
}

回答by Chad

Looking for a similar solution, I came across SharePoint SUSHIon CodePlex. I haven't tried it yet, but it looks like it'll do what you're looking for. Or if you really want to write your own, you could check out the code and see how they're doing it.

在寻找类似的解决方案时,我在 CodePlex 上遇到了SharePoint SUSHI。我还没有尝试过,但看起来它会做你想要的。或者,如果您真的想编写自己的代码,您可以查看代码,看看他们是如何做到的。

回答by Randy H.

I know this question is really old, but since it's still one of the first/only results I thought I would share the solution I found that uses SharePoint search to solve the problem. Not only is it really fast, you can tweak the query to your liking or even create a custom search scope to limit the results.

我知道这个问题真的很老,但由于它仍然是第一个/唯一的结果之一,我想我会分享我发现的使用 SharePoint 搜索来解决问题的解决方案。它不仅非常快,您还可以根据自己的喜好调整查询,甚至可以创建自定义搜索范围来限制结果。

string queryText = "SELECT url, title " +
                    "FROM Scope() " +
                    "WHERE \"Scope\" = 'All Sites' " +
                    "AND (ContentClass = 'STS_Site' OR ContentClass = 'STS_Web')";

SearchServiceApplicationProxy proxy = (SearchServiceApplicationProxy)SearchServiceApplicationProxy.GetProxy(SPServiceContext.GetContext(SPContext.Current.Site));
FullTextSqlQuery searchQuery = new FullTextSqlQuery(proxy);
searchQuery.ResultsProvider = SearchProvider.Default;
searchQuery.ResultTypes = ResultType.RelevantResults;
searchQuery.EnableStemming = false;
searchQuery.TrimDuplicates = true;
searchQuery.QueryText = queryText;
searchQuery.RowLimit = 1000; 
ResultTableCollection results = searchQuery.Execute();
ResultTable result = results[ResultType.RelevantResults];

while (result.Read())
{
    string url = result.GetString(0);
    string title = result.GetString(1);

    ...
}

The query above can also be passed to /vti_bin/search.asmx, but that's a little more complicated. More info can be found here: http://msdn.microsoft.com/en-us/library/ee872313.aspx

上面的查询也可以传递给/vti_bin/search.asmx,但这有点复杂。更多信息可以在这里找到:http: //msdn.microsoft.com/en-us/library/ee872313.aspx

回答by Jason Faulkner

Using the SharePoint .NET SDK, I was able to pull all sites the logged in user has access to by sending a query against the WebTemplate:GROUPkeyword.

使用 SharePoint .NET SDK,我能够通过发送针对WebTemplate:GROUP关键字的查询来提取登录用户有权访问的所有站点。

Here is my working example against SharePoint online (at time of this writing called Office 365 SharePoint):

这是我针对 SharePoint Online 的工作示例(在撰写本文时称为 Office 365 SharePoint):

using Microsoft.SharePoint.Client;
using Microsoft.SharePoint.Client.Search.Query;

// Connection to SharePoint.
var context = new ClientContext(url);

var securePassword = new System.Security.SecureString();
foreach (var c in password.ToCharArray())
{
    securePassword.AppendChar(c);
}
context.Credentials = new SharePointOnlineCredentials(username, securePassword);

// Pull sites the user has access to.
var query = new KeywordQuery(context);
query.QueryText = "WebTemplate:GROUP";
query.SelectProperties.Add("Title");
query.SelectProperties.Add("Path");
query.RowsPerPage = 1000;

var results = new SearchExecutor(context).ExecuteQuery(query);
context.ExecuteQuery();

// Process results...
// (May want to add some error/null condition checks.)
var resultTable = results.Value.First();
foreach (var result in resultTable.ResultRows)
{
    Console.WriteLine(string.Format("Title: {0} -- Path: {1}",
        result["Title"].ToString(),
        result["Path"].ToString()));
}