Click here to Skip to main content
15,884,537 members
Articles / Programming Languages / C#
Tip/Trick

Squadron - Copy Items

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
20 Dec 2012CPOL4 min read 6.9K   2  
In this article we explore the Copy Items functionality of Squadron.

Introduction

In this article we can explore a new feature integration to our Squadron Tool for SharePoint 2010.

Image 1

Copy Items

The Copy Items functionality is simple as the name implies:

It copies list items from a source server list to a destination server list.

It helps us in the following ways:

  1. Quick creation of lists from server to another.
  2. Duplication of items if the lists are same.
  3. Multiple list copying.

We can use the tool to copy multiple lists across sites..

Image 2

Squadron 2010 is a utility add-in application for SharePoint 2010. It is available for download from: http://squadron2010.codeplex.com/.

The method performs the following:

  1. Accepts a source list item and destination list.
  2. Creates a new list item inside a destination list
  3. Iterates over each field of the source item and copies the property to the destination item.
  4. Discards field names of attachments.
  5. Updates the list item on the end of method.

Start using the tool

You can download the tool from the above location.

Step 1: Execute the tool

You will get the following screen on execution:

Image 3

Step 2: Click copy items

Click on the Copy Items item from the left pane and you will see the following add-in:

Image 4

Step 3: Source server connect

As the screen implies, you have to do the following steps of actions:

Source server connect: In this step input the URL of the source server. You can enter the user name if higher permissions are needed. (By default it is SHAREPOINT\system.) For the time being enter your SharePoint site URL here: http://localhost. Then click the Refresh button. You will get the Lists loaded in the Step 2 box as shown below.

Image 5

Step 4: Source List Select

Check those lists which you wish to copy. For the time being I have checked two lists.

Image 6

Please note that the number of list items is shown within parenthesis.

Step 5: Destination Server Connect

In this step, input the URL of the destination server. You can enter the user name if higher permissions are needed. (By default it is SHAREPOINT\system.)

You can choose the same site URL, if you wish to create duplicate items. You can choose a different site URL to create the list and copy the items. Please note that the new list created will have the same List Template and Quick Launch property as the source list.

For the time being I have created another subsite inside the local host. My URL would be: http://localhost/newsite.

After entering the URL, click the Refresh button to the right.

Image 7

You will see the section populated with the source and destination list names. Please note that if there is no destination list, a new list will be created and items will be copied. If there is a destination list with an incompatible structure, the copying for the particular list won't be performed.

Step 5: Copy Items

This step performs the Copy operation. You can see the progress through the progress bar and the log message box. Click on the Copy button to start copying.

Image 8

Within a few seconds the copy operation will be completed and you will get the status bars updated as shown above. The log box will be displaying the copy operation details.

For reference purpose, I have attached the source code for Copy Items along with this article.

Step 6: Verifying the data

Now we can go back to the new SharePoint site to verify the lists and data. You can see that there are two new lists created and items are copied.

Following is the data for the Contacts list in the destination site.

Image 9

Following is the data for Tasks 1 list in the destination site.

Image 10

The current functionality only allows List items copying. In future Library items copying will be included.

Underlying Code

The underlying code that performs the Copy Items functionality is the following method:

C#
private void CopyItem(SPListItem item, SPList destList)
{
    SPListItem newItem = destList.Items.Add();

    for (int i = 0; i < item.Fields.Count; i++)
        if ((!newItem.Fields[i].ReadOnlyField) 
                && (newItem.Fields[i].InternalName != "Attachments"))
            newItem[newItem.Fields[i].InternalName] = item[newItem.Fields[i].InternalName];
    newItem.Update();
}

In case the destination list is not existing, we need to create a new list with the same structure. Following is the code that performs the same.

C#
private SPList CreateList(SPWeb sourceWeb, SPWeb destinationWeb, string stitle, string dTitle)
{
    SPList slist = sourceWeb.Lists[stitle];
    Guid dguid = destinationWeb.Lists.Add(dTitle, slist.Description, slist.BaseTemplate);

    SPList dlist = destinationWeb.Lists[dguid];
    dlist.OnQuickLaunch = slist.OnQuickLaunch;
    dlist.Update();

    return dlist;
}

Copying between incompatible list structures is not allowed. The check is performed by the following code:

C#
private bool IsStructureValid()
{
    SPList sourceList = SourceWeb.Lists[SourceListName];
    SPList destList = DestinationWeb.Lists[DestinationListName];

    for (int i = 0; i < sourceList.Fields.Count; i++)
    {
        string fieldName = sourceList.Fields[i].Title;

        if (!destList.Fields.Cast<SPField>().Any(f => f.Title == fieldName))
            return false;

        if (destList.Fields[fieldName].Type != sourceList.Fields[i].Type)
            return false;
    }
    return true;
}

This concludes our article on Copy Items add-in for Squadron. I hope this add-in will be useful.

References

Summary

In this article we have explored the Copy Items functionality of Squadron. You are free to download the tool and start using it. As always I am listening for new features integration into Squadron.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect
United States United States
Jean Paul is a Microsoft MVP and Architect with 12+ years of experience. He is very much passionate in programming and his core skills are SharePoint, ASP.NET & C#.

In the academic side he do hold a BS in Computer Science & MBA. In the certification side he holds MCPD & MCTS spanning from .Net Fundamentals to SQL Server.

Most of the free time he will be doing technical activities like researching solutions, writing articles, resolving forum problems etc. He believes quality & satisfaction goes hand in hand.

You can find some of his work over here. He blogs at http://jeanpaulva.com

Comments and Discussions

 
-- There are no messages in this forum --