C# 如何使rest调用返回所有结果或总计数(Sharepoint rest API)
我需要知道一个查询的结果总数,下面的代码适用于100个项目,但当我将其更改为1000时,会出现500个内部服务器错误,因为这是sharepoint online,我想他们可能会有某种保护,以避免出现非常大的响应,如果您允许每个租户都这样做,这些响应可能会挂起服务器 我想我必须迭代所有结果,直到得到计数 有人能告诉我如何迭代所有结果直到得到总数吗C# 如何使rest调用返回所有结果或总计数(Sharepoint rest API),c#,rest,sharepoint,visual-studio-2013,C#,Rest,Sharepoint,Visual Studio 2013,我需要知道一个查询的结果总数,下面的代码适用于100个项目,但当我将其更改为1000时,会出现500个内部服务器错误,因为这是sharepoint online,我想他们可能会有某种保护,以避免出现非常大的响应,如果您允许每个租户都这样做,这些响应可能会挂起服务器 我想我必须迭代所有结果,直到得到计数 有人能告诉我如何迭代所有结果直到得到总数吗 static void Main(string[] args) { var config = (NameValueCollec
static void Main(string[] args)
{
var config = (NameValueCollection)ConfigurationManager.GetSection("Sites");
string user=ConfigurationManager.AppSettings["username"];
string pass = ConfigurationManager.AppSettings["password"];
char[] passwordChars = pass.ToCharArray();
SecureString password = new SecureString();
foreach (char c in passwordChars)
{
password.AppendChar(c);
}
foreach (var key in config.Keys)
{
Uri siteUri = new Uri(config.GetValues(key as string)[0]);
using (var context = new ClientContext(siteUri.ToString()))
{
context.Credentials = new SharePointOnlineCredentials(user, password);
string fullurl = siteUri + "/_api/search/query?querytext='pptx'&rowlimit=100";
HttpWebRequest endpointRequest = (HttpWebRequest)HttpWebRequest.Create(fullurl);
endpointRequest.Credentials = context.Credentials;
endpointRequest.Method = "GET";
endpointRequest.Accept = "application/atom+xml";
endpointRequest.ContentType = "application/atom+xml;type=entry";
endpointRequest.Headers.Add("X-FORMS_BASED_AUTH_ACCEPTED: f");
HttpWebResponse endpointResponse = (HttpWebResponse)endpointRequest.GetResponse();
// process response..
XDocument oDataXML = XDocument.Load(endpointResponse.GetResponseStream(), LoadOptions.None);
XNamespace atom = "http://www.w3.org/2005/Atom";
XNamespace d = "http://schemas.microsoft.com/ado/2007/08/dataservices";
XNamespace m = "http://schemas.microsoft.com/ado/2007/08/dataservices/metadata";
List<XElement> items = oDataXML.Descendants(d + "query")
.Elements(d + "PrimaryQueryResult")
.Elements(d + "RelevantResults")
.Elements(d + "Table")
.Elements(d + "Rows")
.Elements(d + "element")
.ToList();
// N.B. there might be a more elegant/efficient way of extracting the values from the (slightly awkward) XML than this..
var searchResults = from item in items
select new
{
Title = item.Element(d + "Cells").Descendants(d + "Key").First(a => a.Value == "Title").Parent.Element(d + "Value").Value,
Author = item.Element(d + "Cells").Descendants(d + "Key").First(a => a.Value == "Author").Parent.Element(d + "Value").Value,
HitHighlightedSummary = item.Element(d + "Cells").Descendants(d + "Key").First(a => a.Value == "HitHighlightedSummary").Parent.Element(d + "Value").Value,
Path = item.Element(d + "Cells").Descendants(d + "Key").First(a => a.Value == "Path").Parent.Element(d + "Value").Value
};
Console.WriteLine(string.Format("Run at:{0}, Count:{1}",DateTime.Now.ToLongDateString(), items.Count);
}
}
}
static void Main(字符串[]args)
{
var config=(NameValueCollection)ConfigurationManager.GetSection(“站点”);
字符串user=ConfigurationManager.AppSettings[“username”];
string pass=ConfigurationManager.AppSettings[“password”];
char[]passwordChars=pass.tocharray();
SecureString密码=新SecureString();
foreach(passwordChars中的字符c)
{
密码。AppendChar(c);
}
foreach(config.Keys中的var键)
{
Uri siteUri=newURI(config.GetValues(键为字符串)[0]);
使用(var context=newclientcontext(siteUri.ToString()))
{
context.Credentials=新的SharePointOnlineCredentials(用户、密码);
字符串fullurl=siteUri+“/_api/search/query?querytext='pptx'&rowlimit=100”;
HttpWebRequest endpointRequest=(HttpWebRequest)HttpWebRequest.Create(fullurl);
endpointRequest.Credentials=上下文.Credentials;
endpointRequest.Method=“GET”;
endpointRequest.Accept=“application/atom+xml”;
endpointRequest.ContentType=“应用程序/atom+xml;type=entry”;
endpointRequest.Headers.Add(“X-FORMS_-BASED_-AUTH_-ACCEPTED:f”);
HttpWebResponse endpointResponse=(HttpWebResponse)endpointRequest.GetResponse();
//过程响应。。
XDocument oDataXML=XDocument.Load(endpointResponse.GetResponseStream(),LoadOptions.None);
XAtom=”http://www.w3.org/2005/Atom";
xd=”http://schemas.microsoft.com/ado/2007/08/dataservices";
Xm=”http://schemas.microsoft.com/ado/2007/08/dataservices/metadata";
列表项=oDataXML.substands(d+“查询”)
.元素(d+“PrimaryQueryResult”)
.要素(d+“相关结果”)
.要素(d+“表格”)
.元素(d+“行”)
.要素(d+“要素”)
.ToList();
//注意:可能有一种更优雅、更高效的方法可以从(稍微笨拙的)XML中提取值。。
var searchResults=来自项目中的项目
选择新的
{
Title=item.Element(d+“Cells”).subjects(d+“Key”).First(a=>a.Value==“Title”).Parent.Element(d+“Value”).Value,
Author=item.Element(d+“Cells”).subjects(d+“Key”).First(a=>a.Value==“Author”).Parent.Element(d+“Value”).Value,
HighlightedSummary=item.Element(d+“Cells”)。子体(d+“Key”)。首先(a=>a.Value==“HighlightedSummary”)。父元素(d+“Value”)。值,
Path=item.Element(d+“Cells”).subjects(d+“Key”).First(a=>a.Value==“Path”).Parent.Element(d+“Value”).Value
};
WriteLine(string.Format(“运行于:{0},计数:{1}”,DateTime.Now.ToLongDateString(),items.Count);
}
}
}
如果我理解正确,我认为您不需要迭代整个查询来获取计数。只需在查询中调用.count()。从您的示例中,我无法确切确定您试图获取计数的部分。但是,作为示例,请在创建searchResults查询后添加此行:
var count = searchResults.Count();
SharePoint REST搜索响应包含返回总结果计数的元素
/query/PrimaryQueryResult/RelevantResults/TotalRows
例子
您可以使用以下LINQ to XML返回TotalRows
元素值:
var totalResultsCount = oDataXML.Descendants(XName.Get("TotalRows", d.ToString())).FirstOrDefault().Value;
这只返回10个项目,但实际结果是有520个结果。search rest api允许限制返回结果的nr,下面的屏幕截图可以看到,如果有500个以上的项目,我可以设置最大值500,这意味着我必须翻页结果,并以某种方式获得总计数。如果我将rowlimit设置为5,TotalRows会是5吗或者如果没有限制(例如1027),则为总行数?