簡體   English   中英

如何在.asmx Web服務中創建ClientContext

[英]How to create ClientContext in .asmx web service

我成功連線

list.Credentials = new System.Net.NetworkCredential("domain\\\\username", "password");

但無法將ClientContext加入web service :我收到錯誤401。如何使用此代碼將新項目添加到遠程SPSite上的SPList中? 我需要解決什么? 謝謝。

public void UpdateSPList(string Title)
{
    using (AuthenticationSvc.Authentication authSvc = new AuthenticationSvc.Authentication())
    {
        try
        {
            using (ListsSvc.Lists list = new ListsSvc.Lists())
            {
                list.Url = @"http://example-site.com/_vti_bin/Lists.asmx";
                list.CookieContainer = new System.Net.CookieContainer();
                list.AllowAutoRedirect = true;
                list.PreAuthenticate = true;

                list.Credentials = new System.Net.NetworkCredential("domain\\username", "password"); 

                string siteUrl = "http://example-site.com";
                ClientContext context = new ClientContext(siteUrl);

                List announcementsList = context.Web.Lists.GetByTitle("ListName");
                ListItemCreationInformation itemCreateInfo = new ListItemCreationInformation();
                ListItem newItem = announcementsList.AddItem(itemCreateInfo);
                newItem["Title"] = "New Item";
                newItem.Update();
                context.ExecuteQuery();
            }
        }
        catch (Exception ex)
        {
            string errorEntry = ex.Message;
        }

    }
}

您可以使用UpdateListItems方法將新項目插入SharePoint列表。 您必須將cmd屬性設置為“新建”。

public static XmlNode UpdateListItemInsert()
{
            listservice.Lists listProxy = new listservice.Lists();

            string xml = "<Batch OnError='Continue'><Method ID='1' Cmd='New'><Field Name='ID'/><Field Name='usercol'>-1;#BASESMCDEV2\\testmoss</Field></Method><Method ID='2' Cmd='New'><Field Name='ID'/><Field Name='usercol'>-1;#BASESMCDEV2\\testmoss</Field></Method></Batch>";


            XmlDocument doc = new XmlDocument();
            doc.LoadXml(xml);

            XmlNode batchNode = doc.SelectSingleNode("//Batch");


            listProxy.Url = "http://basesmcdev2/sites/tester1/_vti_bin/lists.asmx";
            listProxy.UseDefaultCredentials = true;

            XmlNode resultNode = listProxy.UpdateListItems("custom1", batchNode);

            XElement e = XElement.Parse(resultNode.OuterXml);
            var id = from t in e.Descendants().Attributes("ows_ID") select t.Value;


            return resultNode;

}

更多參考:

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM