다음을 통해 공유


Resolution: Trouble setting appropriate permissions on SharePoint user

We wanted to setup Created by and Modified by fields for each and every item in the site programmatically without using STSADM command.

Resolution

Through object model you can back up the created by and modified by fields.

You can set the values for the created by and modified by fields using the below code snippet.

private void button3_Click(object sender, EventArgs e)
        {
            SPSite oSite = new SPSite("<https://terminator:94/sites/Migration/>");
            SPWeb oWeb = oSite.OpenWeb();
            SPList oList = oWeb.Lists["TestList"];
            SPListItemCollection oListCollection = oList.Items;
            foreach (SPListItem oListItem in oListCollection)
            {
                SPFieldUserValue oUser = new SPFieldUserValue(oWeb, oWeb.CurrentUser.ID, oWeb.CurrentUser.LoginName);
                oListItem["Author"] = oUser;//created by column
                oListItem["Editor"] = oUser;//modified by column
                oListItem.Update();               
            }
            oWeb.Update();
         }

You can access the SPListItem object. Using this object you will be able to retrieve the information of all the fields (Ex. created by, modified by, Id etc..) of the list item.

Again you can retrieve the user information through OM. You can check the condition to get the user you want.
Something like this…
SPUserCollection uc = web.Users;
//condition
uc[i].ID;
uc[i].LoginName;

 

As per my understanding, possible ways to achieve this task should be

1.    Side by side approach

First you should restore the site without created by and modified by fields, then access the source site, loop through each list, loop through each item in the list, fetch the created by and modified by values and set those values for the related item in the destination site.

2.    Collect everything and then restore at once

Store everything first, create an XML (or whatever else you feel suitable) file with the created by and modified by fields of the each list item under the lists and execute the code which can read the XML and set the created by and modified by at the appropriate list items in destination site.

 

Questions

1.    Can we get the relative URL or ID of a list item, then look up the list item in the second site collection by that URL? For example, we have the list item that can be viewed at <https://server/Lists/listname/dispform.aspx?ID=2> - will list item 2 on the imported list be the same item as on the original list? Additionally, can we get /Lists/listname/?ID=2 at a code level, then match that back across to the other site collection, setting the author and editor field appropriately?
Ans.
It seems possible, however I think this is not the good way of accessing the items. As long as you keep restoring the items in the same order to your destination sites, it will maintain the ID. But, you never know when you will lose the track. Example. In your source you might have ids in order 1, 2, 3, 6, 9, 10 because of deleting some of the items in between where in your destination site while restoring it’s going to be straight 1,2,3,4,5,6,7….

2.    As an alternate method, is there a backup method that you know of that preserves item/list GUIDs but not security data, so we can simply match the GUIDs? Can we even look up list items by database GUID, in this case?
Ans.
Through SharePoint OM , you can access the item’s GUID but I don’t think so you can set the GUID for an item.

3.    The Author and Editor list fields reference a SPUser object. Can we set that as an domain login name (domain\login) or do we need the SPUser object to exist, then do a lookup to associate that?
Ans.
It requires User to pre-exist in site. [At least for my code to run which I have provided you]