none
Copying list item with user rights

    Question

  • Hi all!

    I want to copy all splist items and their fields (about 30) from splist to another splist with user rights.

    All suggestions are welcome....

    Thursday, July 18, 2013 8:19 AM

Answers

All replies

  • Hi A_LexP,

    Please refer to my code.

     public void test()
            {
                using (SPSite mysite = new SPSite("http://ghy/"))
                {
                    using (SPWeb myweb = mysite.OpenWeb())
                    {
                        SPList listA = myweb.Lists["A"];
                        SPList listB = myweb.Lists["B"];
                        foreach (SPItem d_item in listA.Items)
                        {
                            SPRoleAssignmentCollection roles = d_item.RoleAssignments;
                            foreach (SPItem item in listB.Items)
                            {
                                item.BreakRoleInheritance(true); //you must set this to true or it wouldn’t work
                                while (item.RoleAssignments.Count > 0)
                                {
                                    item.RoleAssignments.Remove(0);
                                }
                                foreach (SPRoleAssignment role in roles)
                                {
                                    item.RoleAssignments.Add(role);
                                }
                                item.Update();
                            }
                        }
                     
                    }
                }
            }

    Thanks,

    Jack


    Jack Gao
    TechNet Community Support

    Sunday, July 21, 2013 6:09 AM
    Moderator
  • Hi A_LexP,

    You can try another approach using central to administration or Export and Import powershell commands.

    http://www.sharepoint-journey.com/backup-and-restore-sharepoint-2013-part1.html


    My Blog- http://www.sharepoint-journey.com| Twitter
    If a post answers your question, please click "Mark As Answer" on that post and "Vote as Helpful


    Sunday, July 21, 2013 11:10 PM
  • Thanks Jack!

    It good idea. But I want copy items with fields and Role Assigments till one time.

    My code next:

      foreach (SPListItem item in sourceList.Items)
                      {

                            SPListItem newDestItem = destList.Items.Add();

                            //Copy one field to another...

                             newDestItem.Update();

                            SPRoleAssignmentCollection roles = item.RoleAssignments;
                            newDestItem.BreakRoleInheritance(true);
                            while (newDestItem.RoleAssignments.Count > 0)
                            {
                                newDestItem.RoleAssignments.Remove(0);
                            }
                            foreach (SPRoleAssignment role in roles)
                            {
                                newDestItem.RoleAssignments.Add(role);
                            }
                            
                            newDestItem.Update();

                   }

    And I had an error in line " newDestItem.RoleAssignments.Add(role);":

    {"Value does not fall within the expected range."}

    Monday, July 22, 2013 11:14 AM
  • I try this code in Sharepoint 2007
    Monday, July 22, 2013 12:21 PM
  • Hi A_LexP,

    Please press F5 in to debug your code in Visual studio, in fact I find your newDestItem is lack of some necessary data, for example “Title”.  Or you can refer to the following link assign the permission.

    http://sridharu.blogspot.in/2008/12/sharepoint-assigning-permission.html#!/2008/12/sharepoint-assigning-permission.html

    Thanks,

    Jack


    Jack Gao
    TechNet Community Support

    Tuesday, July 23, 2013 8:17 AM
    Moderator