gpt4 book ai didi

c# - 如何使用 C sharp 中的 Web 服务在共享点列表中添加新项目

转载 作者:行者123 更新时间:2023-11-30 22:46:04 25 4
gpt4 key购买 nike

我正在尝试使用 Web 服务从 c# 中的 winform 应用程序将新项目添加到共享点列表。唯一的结果是,我得到了无用的异常“抛出了‘Microsoft.SharePoint.SoapServer.SoapServerException’类型的异常。”

我有一个名为 WebSrvRef 的网络引用 http://server/site/subsite/_vti_bin/Lists.asmx

还有这段代码:

        XmlDocument xmlDoc;
XmlElement elBatch;
XmlNode ndReturn;
string[] sValues;
string sListGUID;
string sViewGUID;

if (lstResults.Items.Count < 1)
{
MessageBox.Show("Unable to Add To SharePoint\n" +
"No test file processed. The list is blank.",
"Add To SharePoint", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
return;
}

WebSrvRef.Lists listService = new WebSrvRef.Lists();
sViewGUID = "{xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx}"; // Test List View GUID
sListGUID = "{xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx}"; // Test List GUID

listService.Credentials= System.Net.CredentialCache.DefaultCredentials;

frmAddToSharePoint dlgAddSharePoint = new frmAddToSharePoint();
if (dlgAddSharePoint.ShowDialog() == DialogResult.Cancel)
{
dlgAddSharePoint.Dispose();
listService.Dispose();
return;
}

sValues = dlgAddSharePoint.Tag.ToString().Split('~');
dlgAddSharePoint.Dispose();

string strBatch = "<Method ID='1' Cmd='New'>" +
"<Field Name='Client#'>" + sValues[0] + "</Field>" +
"<Field Name='Company'>" + sValues[1] + "</Field>" +
"<Field Name='Contact Name'>" + sValues[2] + "</Field>" +
"<Field Name='Phone Number'>" + sValues[3] + "</Field>" +
"<Field Name='Brand'>" + sValues[4] + "</Field>" +
"<Field Name='Model'>" + sValues[5] + "</Field>" +
"<Field Name='DPI'>" + sValues[6] + "</Field>" +
"<Field Name='Color'>" + sValues[7] + "</Field>" +
"<Field Name='Compression'>" + sValues[8] + "</Field>" +
"<Field Name='Value % 1'>" + (((float)lstResults.Groups["Value 1"].Tag)*100).ToString("##0.00") + "</Field>" +
"<Field Name='Value % 2'>" + (((float)lstResults.Groups["Value 2"].Tag)*100).ToString("##0.00") + "</Field>" +
"<Field Name='Value % 3'>" + (((float)lstResults.Groups["Value 3"].Tag)*100).ToString("##0.00") + "</Field>" +
"<Field Name='Value % 4'>" + (((float)lstResults.Groups["Value 4"].Tag)*100).ToString("##0.00") + "</Field>" +
"<Field Name='Value % 5'>" + (((float)lstResults.Groups["Value 5"].Tag)*100).ToString("##0.00") + "</Field>" +
"<Field Name='Comments'></Field>" +
"<Field Name='Overall'>" + (fTotalScore*100).ToString("##0.00") + "</Field>" +
"<Field Name='Average'>" + (fTotalAvg * 100).ToString("##0.00") + "</Field>" +
"<Field Name='Transfered'>" + sValues[9] + "</Field>" +
"<Field Name='Notes'>" + sValues[10] + "</Field>" +
"<Field Name='Resolved'>" + sValues[11] + "</Field>" +
"</Method>";

try
{
xmlDoc = new System.Xml.XmlDocument();
elBatch = xmlDoc.CreateElement("Batch");

elBatch.SetAttribute("OnError", "Continue");
elBatch.SetAttribute("ListVersion", "1");
elBatch.SetAttribute("ViewName", sViewGUID);

strBatch = strBatch.Replace("&", "&amp;");
elBatch.InnerXml = strBatch;

ndReturn = listService.UpdateListItems(sListGUID, elBatch);

MessageBox.Show(ndReturn.OuterXml);
listService.Dispose();
}
catch(Exception Ex)
{
MessageBox.Show(Ex.Message +
"\n\nSource\n" + Ex.Source +
"\n\nTargetSite\n" + Ex.TargetSite +
"\n\nStackTrace\n" + Ex.StackTrace,
"Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
listService.Dispose();
}

我做错了什么?我错过了什么?请帮忙!!

弗兰克

最佳答案

这很可能是由于这条线:

listService.Credentials= System.Net.CredentialCache.DefaultCredentials;

像这样将其更改为设置的用户名/密码:

listService.Credentials = new NetworkCredential("UserID", "Password");

确保用户 ID/密码有权创建列表等。

关于c# - 如何使用 C sharp 中的 Web 服务在共享点列表中添加新项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2828729/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com