Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

I have document library x on my site. I need to move it by code to a subsite with the same name.

Probably there is no way to do it by code, because I couldnt find an example like this.

Update 1: I havent tested it yet. But should do the trick?? its a custom workflow activity.

 public MoveDocumentLibrary()
        {
            InitializeComponent();
        }

        public static DependencyProperty SourceUrlProperty = DependencyProperty.Register("SourceUrl", typeof(string), typeof(CreateDocumentLibrary), new PropertyMetadata(""));

        [DescriptionAttribute("Source Url of  current site")]
        [BrowsableAttribute(true)]
        [DesignerSerializationVisibilityAttribute(DesignerSerializationVisibility.Visible)]
        [ValidationOption(ValidationOption.Optional)]
        public string SourceUrl
        {
            get
            {
                return ((string)(base.GetValue(SourceUrlProperty)));
            }
            set
            {
                base.SetValue(SourceUrlProperty, value);
            }
        }

        public static DependencyProperty DestinationUrlProperty = DependencyProperty.Register("DestinationUrl", typeof(string), typeof(CreateDocumentLibrary), new PropertyMetadata(""));

        [DescriptionAttribute("Destination Url where document library will be created")]
        [BrowsableAttribute(true)]
        [DesignerSerializationVisibilityAttribute(DesignerSerializationVisibility.Visible)]
        [ValidationOption(ValidationOption.Optional)]
        public string DestinationUrl
        {
            get
            {
                return ((string)(base.GetValue(SourceUrlProperty)));
            }
            set
            {
                base.SetValue(SourceUrlProperty, value);
            }
        }

        public static DependencyProperty NameDocLibraryProperty = DependencyProperty.Register("SiteUrl", typeof(string), typeof(CreateDocumentLibrary), new PropertyMetadata(""));

        [DescriptionAttribute("Name of document library to be moved")]
        [BrowsableAttribute(true)]
        [DesignerSerializationVisibilityAttribute(DesignerSerializationVisibility.Visible)]
        [ValidationOption(ValidationOption.Optional)]
        public string NameDocLibrary
        {
            get
            {
                return ((string)(base.GetValue(NameDocLibraryProperty)));
            }
            set
            {
                base.SetValue(NameDocLibraryProperty, value);
            }
        }


        public static DependencyProperty DocumentLibraryDestinationUrlProperty = DependencyProperty.Register("DocumentLibraryUrl", typeof(string), typeof(CreateDocumentLibrary), new PropertyMetadata(""));

        [DescriptionAttribute("Destination Url for new DocumentLibrary")]
        [BrowsableAttribute(true)]
        [DesignerSerializationVisibilityAttribute(DesignerSerializationVisibility.Visible)]
        [ValidationOption(ValidationOption.Optional)]
        public string DocumentLibraryDestinationUrl
        {
            get
            {
                return ((string)(base.GetValue(DocumentLibraryDestinationUrlProperty)));
            }
            set
            {
                base.SetValue(DocumentLibraryDestinationUrlProperty, value);
            }
        }


        protected override ActivityExecutionStatus Execute(ActivityExecutionContext executionContext)
        {
            MoveDocLibrary();
            return ActivityExecutionStatus.Closed;
        }


        /// <summary>
        /// It moves the document library to a new collection
        /// </summary>
        private void MoveDocLibrary()
        {
            try
            {
                using (SPSite oSPSite = new SPSite(SourceUrl))
                {
                    using (SPWeb oSPWeb = oSPSite.RootWeb)
                    {
                        SPFolder oFolder = oSPWeb.GetFolder(NameDocLibrary);
                        SPFileCollection collFile = oFolder.Files;

                        List<SPFile> listFiles = new List<SPFile>(collFile.Count);
                        foreach (SPFile oFile in collFile)
                        {
                            listFiles.Add(oFile);
                        }
                        Guid ID = new Guid();
                        if (oSPWeb.Lists[NameDocLibrary] != null)
                        {
                            ID = oSPWeb.Lists.Add(NameDocLibrary, NameDocLibrary, SPListTemplateType.DocumentLibrary);
                        }
                        else
                        {
                            ID = oSPWeb.Lists[NameDocLibrary].ID;
                        }

                        SPList oSPList = oSPWeb.Lists[ID];
                        foreach (SPFile moveFile in listFiles)
                        {
                            moveFile.MoveTo(DestinationUrl + "/" + NameDocLibrary + "/" + moveFile.Name, true);
                        }
                    }
                }
            }
            catch (Exception)
            {

                throw;
            }
        }
share|improve this question

3 Answers 3

up vote 2 down vote accepted

While I would have to do research to determine if you could flat out move a document library in its entirety, you could instead create a new document library in the required location and then copy all of the files one by one (after all from a technical perspective its the same process).

If you need code examples for these steps let me know and ill put some together.

share|improve this answer
    
is in c#, I just updated the question with some code I made, but I haven tested it yet, do you think I should change something, its a custom activity –  L.V. Sharepoint Architect Mar 20 '12 at 13:41
    
at a glance it looks good, of course i would test it on a dev library rather than a live one. Two things though, First it does not appear that your code would maintain a folder hierarchy, and second 'if (oSPWeb.Lists[NameDocLibrary] != null)'s true condition would be that the library exists correct? In that case you would want to run the code in your else statement, not what you have in your if. –  Zork Mar 20 '12 at 15:49
    
lol yes!!!!!!!! –  L.V. Sharepoint Architect Mar 20 '12 at 22:10

(Not sure if you are looking for C# or PowerShell code solution). The quickest way would be to export the document library using the Export-SPWeb PowerShell command. Use Import-SPWeb for importing the file package (it can also use optional compression. See the -Path and other options in the Export-SPWeb command).

An example showing the use of the above method - Export / Import / Migrate document libraries across sites in SharePoint 2010.

share|improve this answer
    
is in c#, I just updated the question with some code I made, but I haven tested it yet, do you think I should change something, its a custom activity –  L.V. Sharepoint Architect Mar 20 '12 at 13:41

It sounds as though this is a one shot type process. This will likely work as an installed feature but if you are attempting to create a console application to run on the SharePoint server, this will not work. SPFile.CopyTo and SPFile.MoveTo only work in features and web parts. It does not work in console applications, even if they run on the SP server itself.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.