Introduction
In this article we can explore a new feature integration to our Squadron Tool for SharePoint 2010.
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:
- Quick creation of lists from server to another.
- Duplication of items if the lists are same.
- Multiple list copying.
We can use the tool to copy multiple lists across sites..
The method performs the following:
- Accepts a source list item and destination list.
- Creates a new list item inside a destination list
- Iterates over each field of the source item and copies the property to the destination
item.
- Discards field names of attachments.
- 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:
Step 2: Click copy items
Click on the Copy Items item from the left pane and you will see the following add-in:
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.
Step 4: Source List Select
Check those lists which you wish to copy. For the time being I have checked
two lists.
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.
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.
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.
Following is the data for Tasks 1 list in the destination site.
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:
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.
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:
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.
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