我为SharePoint创建一个Web服务以返回两个值,但我无法使用DataTable作为该方法的返回类型。
如何让此方法在List<>
中返回两个差值(差异数据类型)?
[WebMethod(EnableSession=true, Description=" Get All sites in the Site Collection.")]
public List<string> GetAllSites(string InputSitecollectionUrl)
{
List<string> w = new List<string>();
using (SPSite TargetsiteCollection = new SPSite(InputSitecollectionUrl))
{
SPWebCollection allWebs = TargetsiteCollection.AllWebs;
foreach (SPWeb web in allWebs)
{
string WebUrl = web.Url;
string WebTitle = web.Title;
w.Add(WebUrl);
w.Add(WebTitle);
}
}
return w;
}
答案 0 :(得分:2)
您可能想要使用List<string>
List<KeyValuePair<T1, T2>>
var w = new List<KeyValuePair<string, string>>();
foreach (SPWeb web in allWebs)
{
w.Add(new KeyValuePair<string, string>(web.Url, web.Title));
}
return w;
您可以在KeyValuePair类型约束中指定适合您需要的任何类型。
答案 1 :(得分:0)
DataSet set = new DataSet("sites");
DataTable table1 = new DataTable("site");
table1.Columns.Add("SiteUrl");
table1.Columns.Add("SiteTitle");
// Create a DataSet and put both tables in it.
using (SPSite TargetsiteCollection = new SPSite(InputSitecollectionUrl))
{
SPWebCollection allWebs = TargetsiteCollection.AllWebs;
foreach (SPWeb web in allWebs)
{
string WebUrl = web.Url;
string WebTitle = web.Title;
table1.Rows.Add(WebUrl, WebTitle);
}
set.Tables.Add(table1);
}
return set;