如何在运行时为sharepoint生成代理类

时间:2015-02-10 16:59:43

标签: c# sharepoint sharepoint-2010

我有一个sharepoint网站(如下所示),我可以将其添加为服务参考和用于创建列表和编辑列表的代码。

https://sharepoint.eastasia.minesoft.com/sites/UnityTest/StationTest/_vti_bin/lists.asmx

我的代码的问题是它始终硬编码到上面的测试sharepoint站点。如果我想重新使用代码,我觉得我应该在运行时如何获取sharepoint的url并生成代理类..

我的代码如下所示:

        ListsSoapClient client = new ListsSoapClient();            
        client.ClientCredentials.Windows.ClientCredential = System.Net.CredentialCache.DefaultNetworkCredentials;
        client.ClientCredentials.Windows.AllowedImpersonationLevel = System.Security.Principal.TokenImpersonationLevel.Impersonation;
        client.Open();

2 个答案:

答案 0 :(得分:0)

如果能够使代码使用REST端点,则可以在运行时更改URL,然后反序列化返回值:

Uri uri = new Uri("mysharpeointurl/_api/web/Lists/GetByTitle('libraryname')/Items?$select=ID,Title");
        var response = client.GetAsync(uri);
        string text = await response.Result.Content.ReadAsStringAsync();

        DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(Returned.RootObject));
        Returned.RootObject rootObject = null;
        using (var ms = new MemoryStream(Encoding.Unicode.GetBytes(text))) {
            var documentdata = (Returned.RootObject)serializer.ReadObject(ms);
            rootObject = documentdata;
        }

        return rootObject;

这样,您只需在运行时更改URI,但如果两个站点都返回具有数据协定上可用属性的相同对象(Returned.RootObject),您将能够获取数据。通过在REST上执行$ select语句,您只需返回所需内容,并避免通过网络发送额外数据。

答案 1 :(得分:-1)

您可以尝试以下操作。

client.Endpoint.Address = new System.ServiceModel.EndpointAddress(newUrl);