To help learn the SharePoint object model (primarily for SharePoint 2007), I've been working on a class library with a number of useful functions. I'm a junior developer and this is my first C# project of this nature.
With this in mind, how can I improve the class below? What bad habits do I have or what should I study so I can code better?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
using System.Diagnostics;
// todo: Lists
// MoveListItems
// MoveListItemsSiteToSite
// DeleteListItem
// WriteFileMetadata
// AddListEntry
// Throttling on copy methods
namespace DEVTEST.SharePoint
{
/// <summary>
/// Class to encapsulate methods that interact with SharePoint Lists and Libraries
/// </summary>
public class ListsAndItems
{
/// <summary>
/// Access and modify items in SharePoint lists within the same site
/// </summary>
/// <param name="siteURL"></param>
/// <param name="sourceList"></param>
/// <param name="destinationList"></param>
/// <param name="retainMeta"></param>
public static void MoveListItems(string siteURL, string sourceList, string destinationList, bool retainMeta)
{
Console.WriteLine("[{0}] Opening site: {1}", DateTime.Now.ToShortTimeString(), siteURL);
using (var site = new SPSite(siteURL))
{
Console.WriteLine("[{0}] Opened site: {1}", DateTime.Now.ToShortTimeString(), siteURL);
using (var web = site.OpenWeb())
{
Console.WriteLine("[{0}] Web Relative URL is: {1}", DateTime.Now.ToShortTimeString(), web.ServerRelativeUrl);
try
{
// Get your source and destination libraries
var source = web.GetList(web.ServerRelativeUrl + sourceList);
var destination = web.GetList(web.ServerRelativeUrl + destinationList);
Console.WriteLine("[{0}] Source set to: {1}", DateTime.Now.ToShortTimeString(), source);
Console.WriteLine("[{0}] Destination set to: {1}", DateTime.Now.ToShortTimeString(), destination);
// Get the collection of items to move, use source.GetItems(SPQuery) if you want a subset
SPListItemCollection items = source.Items;
// Get the root folder of the destination we'll use this to add the files
SPFolder folder = web.GetFolder(destination.RootFolder.Url);
Console.WriteLine("[{0}] Moving {1} files from {2} to {3} - please wait...", DateTime.Now.ToShortTimeString(),
items.Count, source, destination);
var fileCount = 0;
// Now to move the files and the metadata
foreach (SPListItem item in items)
{
//Get the file associated with the item
SPFile file = item.File;
// Create a new file in the destination library with the same properties
SPFile newFile = folder.Files.Add(folder.Url + "/" + file.Name, file.OpenBinary(), file.Properties, true);
// Optionally copy across the created/modified metadata
if (retainMeta)
{
SPListItem newItem = newFile.Item;
WriteFileMetaDataFiletoFile(item, newItem);
}
// Delete the original version of the file
// todo: make local backup before deleting?
file.Delete();
fileCount++;
}
Console.WriteLine("[{0}] Completed moving {1} files to {2}", DateTime.Now.ToShortTimeString(), fileCount,
destination);
}
catch (System.IO.FileNotFoundException)
{
Console.WriteLine(
"[{0}] Unable to set a location. Please check that paths for source and destination libraries are correct and relative to the site collection.",
DateTime.Now.ToShortTimeString());
throw;
}
catch (Exception ex)
{
Console.WriteLine("[{0}] Exception: {1}", DateTime.Now.ToShortTimeString(), ex);
throw;
}
}
}
}
/// <summary>
/// Access and modify items in SharePoint lists in differing sites
/// </summary>
/// <param name="sourceSiteURL"></param>
/// <param name="sourceList"></param>
/// <param name="destinationSiteURL"></param>
/// <param name="destinationList"></param>
/// <param name="retainMeta"></param>
public static void MoveListItemsSiteToSite(string sourceSiteURL, string sourceList, string destinationSiteURL,
string destinationList, bool retainMeta)
{
Console.WriteLine("[{0}] Opening Source site: {1}", DateTime.Now.ToShortTimeString(), sourceSiteURL);
using (var sourceSite = new SPSite(sourceSiteURL))
{
Console.WriteLine("[{0}] Opened Source site: {1}", DateTime.Now.ToShortTimeString(), sourceSiteURL);
using (var sourceWeb = sourceSite.OpenWeb())
{
Console.WriteLine("[{0}] Source Web Relative URL is: {1}", DateTime.Now.ToShortTimeString(), sourceWeb.ServerRelativeUrl);
try
{
// Get your source library
var source = sourceWeb.GetList(sourceWeb.ServerRelativeUrl + sourceList);
Console.WriteLine("[{0}] Source set to: {1}", DateTime.Now.ToShortTimeString(), source);
// Get the collection of items to move, use source.GetItems(SPQuery) if you want a subset
SPListItemCollection items = source.Items;
var fileCount = 0;
Console.WriteLine("[{0}] Opening Destination site: {1}", DateTime.Now.ToShortTimeString(), destinationSiteURL);
using (var destSite = new SPSite(destinationSiteURL))
{
Console.WriteLine("[{0}] Opened Destination site: {1}", DateTime.Now.ToShortTimeString(), destSite);
using (var destinationWeb = destSite.OpenWeb())
{
Console.WriteLine("[{0}] Destination Web Relative URL is: {1}", DateTime.Now.ToShortTimeString(),
destinationWeb.ServerRelativeUrl);
// get destination library
var destination = destinationWeb.GetList(destinationWeb.ServerRelativeUrl + destinationList);
Console.WriteLine("[{0}] Destination set to: {1}", DateTime.Now.ToShortTimeString(), destination);
// Get the root folder of the destination we'll use this to add the files
SPFolder destinationFolder = destinationWeb.GetFolder(destination.RootFolder.Url);
Console.WriteLine("[{0}] Moving {1} files from {2} to {3} - please wait...", DateTime.Now.ToShortTimeString(),
items.Count, source, destination);
// Now to move the files and the metadata
foreach (SPListItem item in items)
{
//Get the file associated with the item
SPFile file = item.File;
// Create a new file in the destination library with the same properties
SPFile newFile = destinationFolder.Files.Add(destinationFolder.Url + "/" + file.Name, file.OpenBinary(),
file.Properties, true);
// Optionally copy across the created/modified metadata
if (retainMeta)
{
SPListItem newItem = newFile.Item;
WriteFileMetaDataFiletoFile(item, newItem);
}
// Delete the original version of the file
// todo: make local backup before deleting?
file.Delete();
fileCount++;
}
Console.WriteLine("[{0}] Completed moving {1} files to {2}", DateTime.Now.ToShortTimeString(), fileCount,
destination);
}
}
}
catch (System.IO.FileNotFoundException)
{
Console.WriteLine(
"[{0}] Unable to set a location. Please check that paths for source and destination libraries are correct and relative to the site collection.",
DateTime.Now.ToShortTimeString());
throw;
}
catch (Exception ex)
{
Console.WriteLine("[{0}] Exception: {1}", DateTime.Now.ToShortTimeString(), ex);
throw;
}
}
}
}
/// <summary>
/// Overwrite existing meta data for a file with meta data from another file
/// </summary>
/// <param name="sourceItem">Source file to take meta data from</param>
/// <param name="destinationItem">Destination file to write meta data to</param>
public static void WriteFileMetaDataFiletoFile(SPListItem sourceItem, SPListItem destinationItem)
// overwrites a list items meta data with meta data from another file
{
//todo: change to write individual items instead of using source item
destinationItem["Editor"] = sourceItem["Editor"];
destinationItem["Modified"] = sourceItem["Modified"];
destinationItem["Modified By"] = sourceItem["Modified By"];
destinationItem["Author"] = sourceItem["Author"];
destinationItem["Created"] = sourceItem["Created"];
destinationItem["Created By"] = sourceItem["Created By"];
// UpdateOverwriteVersion() will preserve the metadata added above.
destinationItem.UpdateOverwriteVersion();
}
/// <summary>
///
/// </summary>
/// <param name="item"></param>
/// <param name="editor"></param>
/// <param name="modified"></param>
/// <param name="modifiedBy"></param>
/// <param name="author"></param>
/// <param name="created"></param>
/// <param name="createdBy"></param>
public static void WriteFileMetaData(SPList item, string editor, DateTime modified, string modifiedBy, string author, DateTime created, string createdBy)
{
// todo
}
}
}