Saturday 15 March 2014

Programmatically move items from one document library to another

The following code snippet can be used to move items in one document library to another.
 
  public static void Movefiles(string url, string sourceListName, string destinationListName)
        {
            try
            {
                var site = new SPSite(url);
                SPWeb web = site.OpenWeb();
                SPList itemCollection = web.Lists[sourceListName];
                SPListItem spItem = itemCollection.Items[0];
                SPFile srcFile = web.GetFile(spItem.Url);
                SPList destDocLib = web.Lists[destinationListName];
                srcFile.MoveTo(destDocLib + "/" + srcFile.Name, true);
                Console.WriteLine("Document Move Successfull.");
            }
            catch (Exception exception)
            {
                throw exception;
            }
        }


PROGRAMMATICALLY CONFIGURE SEND TO CONNECTIONS

The following code snippet can be used to Create a ‘Send To’ Connection to Send Documents to The Records Center in SharePoint 2013.

 public static void ConfigureSendToConnections(string url)
        {
            try
            {          
                using (var site = new SPSite(url))
                {
                    SPWebApplication webapp = site.WebApplication;
                    var newhost = new SPOfficialFileHost(true);
                    newhost.OfficialFileUrl = new Uri(site.Url + "/records/_vti_bin/officialfile.asmx");
                    newhost.OfficialFileName = "Records Center";
                    newhost.ShowOnSendToMenu = true;
                    newhost.Action = SPOfficialFileAction.Move;
                    webapp.OfficialFileHosts.Add(newhost);
                    webapp.Update();                
                }
            }
            catch (Exception exception)
            {
                throw exception;
            }
        }


1 comment: