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

时间:2012-05-23 19:51:09

标签: c# web-services sharepoint

我为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;
}

2 个答案:

答案 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;