我正在使用通过表单界面创建SharePoint列表项的.NET应用程序。应用程序需要允许用户将附件上载到列表项。我可以创建列表项,但是当使用list.asmx服务addAttachment方法时,我得到一个"尝试使用已经不存在的对象"尝试通过FileUpload控件附加文件时出错,其中文件作为字节数组存储在会话中。
我的代码看起来像这样......
创建列表项:
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;
}
}
FileUpload控制按钮事件:
protected void btnHeadShotUpload_Click(object sender, EventArgs e)
{
if (HeadshotUpload.HasFile)
{
var file = HeadshotUpload.FileBytes;
Session.Add("Headshot", file);
Session.Add("HeadshotFileName", headshotFileName + " - " + HeadshotUpload.FileName);
}
}
附件功能:
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;
}
}
ULS登录SharePoint Server:
SOAP异常:Microsoft.SharePoint.SPException:尝试使用 已不复存在的物体。 (HRESULT的例外情况:0x80030102 (STG_E_REVERTED))---&gt; System.Runtime.InteropServices.COMException (0x80030102):尝试使用已不复存在的对象。 (来自HRESULT的异常:0x80030102(STG_E_REVERTED))
在附件功能中,3228列表项ID是我用于测试的现有列表项。我已验证此列表项确实存在于SharePoint列表中。期望的最终结果是将文件附加到上载附件之前创建的列表项。
任何人都可以提供有关导致此错误的原因的任何见解吗?
答案 0 :(得分:0)
我能够使附件功能正常工作。出于某种原因,SharePoint不喜欢Session中传递给addAttachment函数的数组。将Memorystream传递给addAttachment函数的字节数组参数正常工作。
我不确定为什么这是必要的,但确实解决了这个问题。检查每个字节数组显示它们尽可能相同。