我的任务是编写一份报告,将数据从各种 SharePoint 网站的“存储空间分配”部分中提取出来。我可以通过执行一般的 GET 调用来筛选一般的“文档库”值,但我无法以编程方式获取“列表”值。当我导航到 SharePoint 网站 (*/_layouts/storman.aspx) 时,“文档库”是默认选择。我想我需要发送一个 POST 调用才能将其更改为“列表”[然后我可以抓取值]。创建适当的 POST 调用变得很麻烦,因为 SharePoint 似乎无法识别我的键/值对(或者我可能没有提供所有必要的参数?)。
我试过这段代码,但没有运气 - 只返回“文档库”数据。
using (System.Net.WebClient client = new System.Net.WebClient() { UseDefaultCredentials = true })
{
NameValueCollection myQueryStringCollection = new NameValueCollection();
myQueryStringCollection.Add(queryParameterName, queryParameterValue);
client.QueryString = myQueryStringCollection;
return client.DownloadString(url);
}
我也试过这个(连同其他想法):
private static string GetWebResponse(string url, NameValueCollection parameters)
{
var httpWebRequest = (HttpWebRequest)WebRequest.Create(url);
httpWebRequest.UseDefaultCredentials = true;
httpWebRequest.ContentType = "application/x-www-form-urlencoded";
httpWebRequest.Method = "POST";
var sb = new StringBuilder();
foreach (var key in parameters.AllKeys)
sb.Append(key + "=" + parameters[key] + "&");
sb.Length = sb.Length - 1;
byte[] requestBytes = Encoding.UTF8.GetBytes(sb.ToString());
httpWebRequest.ContentLength = requestBytes.Length;
using (var requestStream = httpWebRequest.GetRequestStream())
{
requestStream.Write(requestBytes, 0, requestBytes.Length);
requestStream.Close();
}
Task<WebResponse> responseTask = Task.Factory.FromAsync<WebResponse>(httpWebRequest.BeginGetResponse, httpWebRequest.EndGetResponse, null);
using (var responseStream = responseTask.Result.GetResponseStream())
{
var reader = new StreamReader(responseStream);
return reader.ReadToEnd();
}
}
查看 _layouts/storman.aspx 页面的源代码,我可以看到我需要发送的名称/值对分别是ct100$PlaceHolderMain$m_filterDropdown和Lists。我通过这个视图源代码确定了这一点:
<select name="ctl00$PlaceHolderMain$m_filterDropdown" id="ctl00_PlaceHolderMain_m_filterDropdown" class="ms-viewselect">
<option selected="selected" value="Document Libraries">Document Libraries</option>
<option value="Documents">Documents</option>
<option value="Lists">Lists</option>
<option value="Recycle Bin">Recycle Bin</option>
</select>
关于如何从此页面获取列表值的任何想法?