Reputation: 153
i am trying to GET the name,title,content type & document tag of document stored in SharePoint document library and EDIT it programmatically using visual studio 2012 with Sandbox Solution.
i have successfully get all of these properties in VS2012 and also updating successfully but when i click on update button it gives Error: File Not Found.
and after that when i goes to my document page then all these properties looks like updated. so, why it throw error every time i click on update button since all properties also updateing successfully.
my code is here:
SPSite oSite = new SPSite("http://<sitename>/");
2: SPWeb oWeb = oSite.OpenWeb();
3: oWeb.AllowUnsafeUpdates = true;
3: SPList oList = oWeb.Lists["Shared Documents"];
4: SPListItem oListItem = oList.Items[0];
5: oListItem.File.CheckOut();
6: oListItem["Name"] = "xyz";
7: oListItem["Title"] = "abc";
8: oListItem["Content_Type"] = "lmn";
9: oListItem["Document_Tag"] = "pqr";
7: oListItem.Update();
8: oListItem.File.CheckIn("file name has been changed");
11: oWeb.AllowUnsafeUpdates =false;
9: oWeb.Dispose();
Upvotes: 1
Views: 7097
Reputation: 1181
Before update the item you need to get it by this way.
using (SPSite oSite = new SPSite("siteUrl"))
{
using (SPWeb oWeb = oSite.OpenWeb())
{
oWeb.AllowUnsafeUpdates = true;
oWeb.Site.AllowUnsafeUpdates = true;
SPList oList = oWeb.Lists["Shared Documents"];
SPFile file = oList.Items.Cast<SPListItem>()
.Select(x => x.File)
.FirstOrDefault();
if (file == null)
{
return false;
}
SPListItem item = file.GetListItem();
if (item.File.Level == SPFileLevel.Checkout)
{
item.File.UndoCheckOut();
}
if (item.File.Level != SPFileLevel.Checkout)
{
item.File.CheckOut();
}
//Do update here
//item["Content_Type"] = "lmn";
item.SystemUpdate(false);
item.File.CheckIn("SystemCheckedin");
item.File.Publish("SystemPublished");
oWeb.AllowUnsafeUpdates = false;
oWeb.Site.AllowUnsafeUpdates = false;
}
}
Upvotes: 1