我在 SharePoint 2007 中以编程方式在文档库中创建项目时遇到问题。
下面是我的代码片段,也许你能指出我的错误是什么:
SPSecurity.RunWithElevatedPrivileges(delegate()
{
using (SPSite site = new SPSite(_url))
{
using (SPWeb web = site.OpenWeb())
{
SPList customList = web.Lists["Test1"];
foreach (SPListItem ltItem in customList.Items)
{
if (ltItem.Attachments != null && ltItem.Attachments.Count > 0)
{
//Get Test1 File Collection
SPFolder folder = web.GetFolder(ltItem.Attachments.UrlPrefix);
SPFileCollection fileColl = folder.Files;
//Get binary data of attachment
SPFile file = ltItem.ParentList.ParentWeb.GetFile(ltItem.Attachments.UrlPrefix + ltItem.Attachments[0]);
byte[] fileData = file.OpenBinary();
//Get Relative URL of attachment destination
string destFile = fileColl.Folder.Url + "/" + file.Name;
web.AllowUnsafeUpdates = true;
//Add attachment into Document Library
SPDocumentLibrary docLib = (SPDocumentLibrary)web.Lists["Test2"];
SPFile file2 = docLib.RootFolder.Files.Add(destFile, fileData, true);
file2.Item.Update();
web.AllowUnsafeUpdates = false;
}
}
}
}
});
我在代码 file2.Item.Update() 的链接处点击了“对象引用未设置为对象的实例”;
提前谢谢你。