DataTable 作为 Web Service asmx for SharePoint 中方法的返回数据类型

本文关键字:方法 返回 数据类型 SharePoint for 作为 Web Service asmx DataTable | 更新日期: 2023-09-27 18:35:04

我创建了一个来自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;
}

DataTable 作为 Web Service asmx for SharePoint 中方法的返回数据类型

与其

返回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;

您可以在键值对类型约束中指定适合您需求的任何类型。

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;