0

How to set UniqueId property of SPListItem programmatically using SSOM?

0

This solution is not recommended but there are certain scenarios which justify the use such as migration.

In the solution bellow I am seting UniqueId to be equal to C483CED9-9546-46B0-BC84-C077658A9899.

using (SPSite siteCol = new SPSite("http://sp2/sites/test"))
{
    using (SPWeb site = siteCol.OpenWeb())
    {
        SPList list = site.Lists["unique"];

        SPListItem item = list.AddItem();
        item["Title"] = "Some item 5";
        MethodInfo dynMethod = item.GetType().GetMethod("UpdateInternal", BindingFlags.NonPublic | BindingFlags.Instance, null,
            new[] { typeof(bool), typeof(bool), typeof(Guid), typeof(bool), typeof(bool), typeof(bool), typeof(bool), typeof(bool), typeof(bool), typeof(bool) }, null);

        dynMethod.Invoke(item, new object[] { false, false, new Guid("{C483CED9-9546-46B0-BC84-C077658A9899}"), true, false, false, false, false, false, false });

        var items = list.GetItems();
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.