.net &引用;试图使用已不存在的对象";lists.asmx出错
我使用的是一个.NET应用程序,它通过表单界面创建SharePoint列表项。应用程序需要允许用户将附件上载到列表项。我可以创建列表项,但是,当使用list.asmx service addAttachment方法时,我在尝试通过FileUpload控件附加文件时出现“尝试使用已不存在的对象”错误,其中文件作为字节数组存储在会话中 我的代码看起来像这样 创建列表项:.net &引用;试图使用已不存在的对象";lists.asmx出错,.net,web-services,sharepoint,sharepoint-2010,.net,Web Services,Sharepoint,Sharepoint 2010,我使用的是一个.NET应用程序,它通过表单界面创建SharePoint列表项。应用程序需要允许用户将附件上载到列表项。我可以创建列表项,但是,当使用list.asmx service addAttachment方法时,我在尝试通过FileUpload控件附加文件时出现“尝试使用已不存在的对象”错误,其中文件作为字节数组存储在会话中 我的代码看起来像这样 创建列表项: protected void CreateListIteminSharepoint() {
protected void CreateListIteminSharepoint()
{
try
{
ServiceReference1.ListsSoapClient proxy = new ServiceReference1.ListsSoapClient();
proxy.ClientCredentials.Windows.ClientCredential = new NetworkCredential();
XElement list = proxy.GetList("MyList");
// assigning field values...
string strBatch = "<Method ID='1' Cmd='New'><Field Name='ID' >New</Field>
// field assignments omitted to shorten code paste
StringReader reader = new StringReader(strBatch);
XElement method = XElement.Load(reader);
XElement element = new XElement("Batch");
element.SetAttributeValue("OnError", "Continue");
element.SetAttributeValue("PreCalc", "TRUE");
element.SetAttributeValue("ListVersion", "1");
element.SetAttributeValue("ViewName", listViewGuid);
element.Add(method);
XElement ndReturn = proxy.UpdateListItems("MyList", element);
// post headshot image attachment - function in code further down the post
if (Session["Headshot"] != null)
{
var headshotFile = (byte[])Session["Headshot"];
UploadDocToSharepoint(headshotFile, (string)Session["HeadshotFileName"]);
}
}
catch (Exception ex)
{
throw;
}
}
附件功能:
public void UploadDocToSharepoint(byte[] sourceFile, string destinationFileName)
{
ServiceReference1.ListsSoapClient proxy = new ServiceReference1.ListsSoapClient();
proxy.ClientCredentials.Windows.ClientCredential = new NetworkCredential();
try
{
string addAttach = proxy.AddAttachment("MyList", "3228",
"image.jpg", sourceFile);
MessageBox.Show(addAttach);
}
catch (System.Web.Services.Protocols.SoapException ex)
{
throw;
}
catch (Exception ex)
{
throw;
}
}
SharePoint服务器上的ULS日志:
SOAP异常:Microsoft.SharePoint.SPException:尝试使用
不复存在的物体。(HRESULT的异常:0x80030102
(STG_E_已还原)--->System.Runtime.InteropServices.COMException
(0x80030102):试图使用已不存在的对象。
(HRESULT的例外:0x80030102(STG_E_已还原))
在附件函数中,3228列表项ID是我用于测试的现有列表项。我已验证SharePoint列表中是否存在此列表项。理想的最终结果是将文件附加到在上载附件之前创建的列表项
是否有人能提供有关导致此错误的原因的见解?我能够使附件功能正常工作。出于某些原因,SharePoint不喜欢传递到addAttachment函数的会话中的数组。将Memorystream传递到addAttachment函数的字节数组参数中工作正常 我不知道为什么这是必要的,但它确实解决了问题。检查每个字节数组后,我发现它们是完全相同的
public void UploadDocToSharepoint(byte[] sourceFile, string destinationFileName)
{
ServiceReference1.ListsSoapClient proxy = new ServiceReference1.ListsSoapClient();
proxy.ClientCredentials.Windows.ClientCredential = new NetworkCredential();
try
{
string addAttach = proxy.AddAttachment("MyList", "3228",
"image.jpg", sourceFile);
MessageBox.Show(addAttach);
}
catch (System.Web.Services.Protocols.SoapException ex)
{
throw;
}
catch (Exception ex)
{
throw;
}
}