2

In the code below I am getting the following Exception :

 Unhandled Exception: Microsoft.SharePoint.SPException: The file Audit/0003 has been modified by contoso\sp_install.

Does the file need to be checked out first? Anyone has got any idea how to solve this? I can't seem to find example on how to add an item to a document set.

SPList list = web.Lists.TryGetList("Audit");
DocumentSet docSet = CreateDocumentSet(list, au.Guid);
SPListItem docSetItem = docSet.Item;

if (list != null)
{
    docSetItem["A1_1_Question"] = au.A1_1_Question;
    docSetItem["A1_2_Question"] = au.A1_2_Question;
    docSetItem.Update();
}

public static DocumentSet CreateDocumentSet(SPList list, string DocumentSetName)
{
    SPContentType docsetCT = list.ContentTypes["Audit"];

    Hashtable properties = new Hashtable();
    properties.Add("DocumentSetDescription", "New Document Set");
    SPFolder parentFolder = list.RootFolder;
    DocumentSet docSet = DocumentSet.Create(parentFolder, DocumentSetName, docsetCT.Id, properties, true);
    return docSet;
}
Daniel
  • 253
  • 2
  • 9
Imir Hoxha
  • 1,905
  • 6
  • 28
  • 55

2 Answers2

1

Please check the internal name or static name of the fields you are updating. This might cause issue.

http://blogs.msdn.com/b/ronalg/archive/2011/08/11/documentsets-documentset-create-and-the-properties-hashtable.aspx

If Checkout is required in the document then you have to Checkout the item before updating and Checkin after updating.

item.CheckOut();
item.Update();
item.CheckIn();

Also check following:

http://social.msdn.microsoft.com/Forums/sharepoint/en-US/7751d24f-e025-4526-882e-f5f273362f8d/document-set-created-programmatically-doesnt-work-properly

Aanchal
  • 7,885
  • 1
  • 15
  • 20
0

Thanks to this Post I was able to solve my issue. this is the code that I used:

DocumentSet newDc = DocumentSet.Create(folder, "Docset", docsetCT.Id, properties, true);

DocumentSet MyDocSet = DocumentSet.GetDocumentSet(list.GetItemById(newDc.Item.ID).Folder);

MyDocSet.Item["MyField"] = "Test";
MyDocSet.Item.Update();
Imir Hoxha
  • 1,905
  • 6
  • 28
  • 55