如何在 .asmx 网络服务中创建 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 属性设置为 'New'.
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;
}
更多参考:
我在线连接成功
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 属性设置为 'New'.
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;
}
更多参考: