Move Replace DocumentSets in SP2010 - grothauser/sharepointsnippets GitHub Wiki

using System; using Microsoft.SharePoint; using Microsoft.SharePoint.WebControls; using System.Collections; using Microsoft.Office.DocumentManagement.DocumentSets; using System.Collections.Generic;

namespace SendToLocationECBM.Layouts.SendToLocationECBM { public partial class sendToLib : LayoutsPageBase { private string listId; int itemId = -1; private string requestURLString; private string sourceURL;

    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            itemId = int.Parse(Request.QueryString["ItemId"]);
        }
        catch (Exception ParseExc)
        {
            Label2.Text = ParseExc.Message;
        }

        string listIdStr = Request.QueryString["ListId"].Remove(0, 1);
        listId = listIdStr.Remove(listIdStr.LastIndexOf('}'));

        requestURLString = SPContext.Current.Site.WebApplication.GetResponseUri(SPContext.Current.Site.Zone).OriginalString + Request.RawUrl;

        int indexOfLastCLC = requestURLString.LastIndexOf("CLC");
        string collUrl = requestURLString.Substring(indexOfLastCLC);
        string siteUrl = requestURLString.Substring(0, indexOfLastCLC);

        int indexOfFirstSlashAfterCLC = collUrl.IndexOf('/');
        string collName = collUrl.Substring(0, indexOfFirstSlashAfterCLC);

        sourceURL = siteUrl + collName;
    }

    protected void EnableSubSelection(Object sender, EventArgs e)
    {
        RadioButton3.Visible = true;
        RadioButton4.Visible = true;
        RadioButton5.Visible = true;
    }

    protected void DisableSubSelection(Object sender, EventArgs e)
    {
        RadioButton3.Visible = false;
        RadioButton4.Visible = false;
        RadioButton5.Visible = false;
    }


    protected void MoveProject(Object sender, EventArgs e)
    {
        if (itemId != -1)
        {
            SPList sourceLibrary = null;
            using (SPSite siteSource = new SPSite(sourceURL))
            {
                using (SPWeb webSource = siteSource.OpenWeb())
                {
                    if (!String.IsNullOrEmpty(listId))
                    {
                        try
                        {
                            Guid guid = new Guid(listId);
                            sourceLibrary = webSource.Lists[guid];
                        }
                        catch (Exception exc)
                        {
                            //write to Log

                        }
                    }

                    string destPath = urltextbox.Value;
                    string destSiteCollection = destPath.Substring(0, destPath.LastIndexOf("CLC") - 1);
                    string destLibraryName = destPath.Substring(destPath.LastIndexOf('/') + 1);

                    using (SPSite site = new SPSite(destPath))
                    {
                        using (SPWeb web = site.OpenWeb())
                        {
                            if (web.Exists)
                            {
                                SPList destLibrary = web.Lists.TryGetList(destLibraryName);
                                SPList companiesDestColl = web.Lists.TryGetList("Companies");
                                SPFolder destFolder = destLibrary.RootFolder;
                                SPContentType docsetCT = destLibrary.ContentTypes["Project"];

                                if (sourceLibrary != null)
                                {
                                    try
                                    {
                                        SPListItem sourceFolder = sourceLibrary.GetItemById(itemId);

                                        String companyMain = sourceFolder["Company - Main"].ToString();
                                        String companyMainName = companyMain.Substring(companyMain.IndexOf('#') + 1);

                                        //if already a company-main folder, check for documentset with same name
                                        SPQuery queryForExistingDocumentSet = new SPQuery();
                                        queryForExistingDocumentSet.Query = "<Where><And>" +
                                            "<Contains><FieldRef Name='Company_x0020__x002d__x0020_Main'/><Value Type='Text'>" + companyMainName + "</Value></Contains>" +
                                            "<Eq><FieldRef Name='LinkFilename'/><Value Type='Text'>" + sourceFolder["LinkFilename"] + "</Value></Eq>" +
                                            "</And></Where>";

                                        SPListItemCollection ProjectInCompanyMainExisting = destLibrary.GetItems(queryForExistingDocumentSet);

                                        SPQuery findLookupValue = new SPQuery();
                                        findLookupValue.Query = "<Where><Eq><FieldRef Name='Title'/><Value Type='Text'>" + companyMainName + "</Value></Eq></Where>";
                                        
                                        SPListItem lookupCompany = companiesDestColl.GetItems(findLookupValue)[0];
                                        ReplaceNewerInLibrary(sourceLibrary, web, destLibrary, destFolder, sourceFolder, lookupCompany, ProjectInCompanyMainExisting[0]);
                                         

                                /*        if (ProjectInCompanyMainExisting.Count == 0 || ProjectInCompanyMainExisting == null)
                                        {
                                            MoveToLibrary(sourceLibrary, web, destLibrary, folder, item, lookupCompany);
                                        }
                                        else
                                        {
                                            bool merge = RadioButton2.Checked;
                                            if (merge)
                                            {
                                                bool newerWins = RadioButton3.Checked;
                                                bool olderWins = RadioButton4.Checked;
                                                bool keepBoth = RadioButton5.Checked;
                                                if (newerWins)
                                                {
                                                    ReplaceNewerInLibrary(sourceLibrary,web,destLibrary,folder,item,lookupCompany, ProjectInCompanyMainExisting[0]);
                                                    //keep newer files
                                                }
                                                else if (olderWins)
                                                {
                                                    //keep older files
                                                }
                                                else
                                                {
                                                    //keep both
                                                }
                                            }
                                            else
                                            {
                                                //replace

                                            }
                                            Label2.Text = "Document Set Already exists in destination folder";
                                        }*/
                                    }
                                    catch (Exception exc)
                                    {
                                        Label2.Text = exc.Message;
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }

    private void ReplaceNewerInLibrary(SPList sourceLibrary, SPWeb web, SPList destLibrary, SPFolder destfolder, SPListItem sourcefolder, SPListItem lookupCompany, SPListItem existingItem)
    {
        //go trough all files in source folder to check if already existing (re-use in replace if older and keep both)
        DocumentSet existingSet = DocumentSet.GetDocumentSet(existingItem.Folder);
       
        foreach (SPFile file in sourcefolder.Folder.Files)
        {

        }
    }

    private void MoveToLibrary(SPList sourceLibrary, SPWeb web, SPList destLibrary, SPFolder folder, SPListItem item, SPListItem lookupCompany)
    {
        if (item.Folder != null)
        {
            DocumentSet documentSet = DocumentSet.GetDocumentSet(item.Folder);
            SPContentTypeId contentTypeId = destLibrary.ContentTypes["Project"].Id;

            byte[] documentSetData = documentSet.Export();
            string documentSetName = documentSet.Item.Name;

            Hashtable properties = item.Properties;
            try
            {
                web.AllowUnsafeUpdates = true;
                DocumentSet result = DocumentSet.Import(documentSetData, documentSetName, folder, contentTypeId, properties, web.CurrentUser);
                if (lookupCompany != null)
                {
                    result.Item["Company - Main"] = new SPFieldLookupValue(lookupCompany.ID, lookupCompany.Title);
                    result.Item.Update();
                }
                sourceLibrary.Items.DeleteItemById(itemId);
            }
            catch (InvalidOperationException invOpExc)
            {
                Label2.Text = invOpExc.Message;
            }
            finally
            {
                web.AllowUnsafeUpdates = false;
            }
        }
    }

    private List<SPListItem> ToList(SPListItemCollection companyMainExisting)
    {
        throw new NotImplementedException();
    }
}

}

⚠️ **GitHub.com Fallback** ⚠️