Monday, July 29, 2013

update listItem file

Hi,


I have an issue with listItem file update ?


I have to update file attached to listItem. But sometimes a new file is created with the default contenttype in the documentset.


Here the code i use.



using (SPSite site = new SPSite(missionOrder.Web.Site.ID))
using (SPWeb web = site.OpenWeb(missionOrder.Web.ID))
{
bool allowUnsafeUpdate = web.AllowUnsafeUpdates;
bool parserEnabled = web.ParserEnabled;

try
{
web.AllowUnsafeUpdates = true;
web.ParserEnabled = false;
web.Update();

SPListItem item = web.Lists[missionOrder.ParentList.ID].GetItemById(missionOrder.ID);

item.File.SaveBinary(docmStream);
item.File.Update();

//fix for eventReceiver
int nbrOftry = 0;
item = web.Lists[missionOrder.ParentList.ID].GetItemById(missionOrder.ID);

while (item.File.ParentFolder.Name.Equals("Technique") == false || nbrOftry == 3)
{
System.Threading.Thread.Sleep(1000);
item = web.Lists[missionOrder.ParentList.ID].GetItemById(missionOrder.ID);
nbrOftry++;
}
//updating metadata and ContentType
SPContentType missionOrderCT = missionOrder.ParentList.ContentTypes["MissionOrder"];

item["ProjIDAffaire"] = affaireID;
item["MissionOrderID"] = missionID;
item["ContentTypeId"] = missionOrderCT.Id.ToString();
item["SynchroVersNomade"] = "True";
item.UpdateOverwriteVersion();

item = web.Lists[missionOrder.ParentList.ID].GetItemById(missionOrder.ID);

//Delete old item version.
try
{
SPListItemVersionCollection coll = item.Versions;

for (int i = 0; i < item.Versions.Count; i++)
{
if (!coll[1].IsCurrentVersion)
coll[1].Delete();
}
}
catch { }


web.ParserEnabled = parserEnabled;
web.AllowUnsafeUpdates = allowUnsafeUpdate;
web.Update();
}
catch (Exception)
{
web.ParserEnabled = parserEnabled;
web.AllowUnsafeUpdates = allowUnsafeUpdate;
web.Update();
}
}



Regards,


J.D.


No comments:

Post a Comment