Wednesday, July 25, 2012

Copy Files and Folder using Web Service


To copy files, folders and sub folders from one library to another library using web service we will make use of three web services.

1. Lists.asmx: To get all folders and lists.
2. DWS.asmx: To create folders
3. Copy.aspx: To copy files from one library to another


Add above mentioned web refrences to your project.

Sample Project


Below is the function to copy All files, Folders and subfolders




        private void GetAllListItemsRecursively(string sourcelistname, string sourceFolderURL, string destlistname, string destFolderURL)
        {
            XmlNode ndListFolders = null;
            XmlNode ndListItems = null;


            XmlDocument xDocFolders = new XmlDocument();
            XmlNode ndQueryFolders = xDocFolders.CreateNode(XmlNodeType.Element, "Query", "");
            XmlNode ndViewFieldsFolders = xDocFolders.CreateNode(XmlNodeType.Element, "ViewFields", "");
            XmlNode ndQueryOptionsFolders = xDocFolders.CreateNode(XmlNodeType.Element, "QueryOptions", "");
            ndQueryOptionsFolders.InnerXml = @"<Folder>" + sourceFolderURL + "</Folder>";
            ndQueryFolders.InnerXml = "";
            ndViewFieldsFolders.InnerXml = "";
            ndQueryFolders.InnerXml = "<Where><Eq><FieldRef Name='FSObjType' /><Value Type='Lookup'>1</Value></Eq></Where>";


            XmlDocument xDocItems = new XmlDocument();
            XmlNode ndQueryItems = xDocItems.CreateNode(XmlNodeType.Element, "Query", "");
            XmlNode ndViewFieldsItems = xDocItems.CreateNode(XmlNodeType.Element, "ViewFields", "");
            XmlNode ndQueryOptionsItems = xDocItems.CreateNode(XmlNodeType.Element, "QueryOptions", "");
            ndQueryOptionsItems.InnerXml = @"<Folder>" + sourceFolderURL + "</Folder>";
            ndQueryItems.InnerXml = "";
            ndViewFieldsItems.InnerXml = "";
            ndQueryItems.InnerXml = "<Where><Eq><FieldRef Name='FSObjType' /><Value Type='Lookup'>0</Value></Eq></Where>";


            try
            {
                ndListFolders = _Lists.GetListItems(
                                    sourcelistname,
                                    "",
                                    ndQueryFolders,
                                    ndViewFieldsFolders,
                                    null,
                                    ndQueryOptionsFolders, null
                    );


                ndListItems = _Lists.GetListItems(
                                   sourcelistname,
                                   "",
                                   ndQueryItems,
                                   ndViewFieldsItems,
                                   null,
                                   ndQueryOptionsItems, null
                   );
            }
            catch (Exception ex)
            {


            }


            //Copy Folders
            if (ndListFolders != null)
            {
                foreach (XmlNode node in ndListFolders.ChildNodes)
                {
                    if (node.Name == "rs:data")
                    {
                        for (int i = 0; i < node.ChildNodes.Count; i++)
                        {
                            if (node.ChildNodes[i].Name == "z:row")
                            {
                                string newfolderName1 = node.ChildNodes[i].Attributes["ows_LinkFilename"].Value;
                                string rootFolder = string.Format("{0}/{1}", destFolderURL, newfolderName1);
                                _Dws.CreateFolder(rootFolder);


                                GetAllListItemsRecursively(sourcelistname, sourceFolderURL + @"\" + newfolderName1, destlistname, rootFolder);
                            }
                        }
                    }
                }
            }


            //Copy Files
            if (ndListItems != null)
            {
                foreach (XmlNode node in ndListItems.ChildNodes)
                {
                    if (node.Name == "rs:data")
                    {
                        for (int i = 0; i < node.ChildNodes.Count; i++)
                        {
                            if (node.ChildNodes[i].Name == "z:row")
                            {
                                string filename = node.ChildNodes[i].Attributes["ows_LinkFilename"].Value;


                                string finalsourceFolderURL = sourceFolderURL.Replace(@"\", "/");
                                if (!finalsourceFolderURL.EndsWith("/"))
                                    finalsourceFolderURL = finalsourceFolderURL + "/";
                                finalsourceFolderURL = siteURL + finalsourceFolderURL + filename;


                                string finaldestFolderURL = destFolderURL.Replace(@"\", "/");
                                if (!finaldestFolderURL.EndsWith("/"))
                                    finaldestFolderURL = finaldestFolderURL + "/";
                                finaldestFolderURL = siteURL + finaldestFolderURL + filename;
                                string[] copyDest = { finaldestFolderURL };


                                CopyResult myCopyResult1 = new CopyResult();
                                CopyResult[] myCopyResultArray = { myCopyResult1 };


                                // string copyResult;
                                try
                                {
                                    _Copy.CopyIntoItemsLocal(finalsourceFolderURL, copyDest, out myCopyResultArray);
                                }
                                catch (Exception exc) { }
                            }
                        }
                    }
                }
            }
        }

Sample Project

Monday, July 23, 2012

SharePoint 2010 Feature Stapling



Feature Stapling allows you to create a feature and then associate it with any site definition without ever touching the site definition files themselves. Your feature will be activated when the site is provisioned.


Stapling basically consist of two features:
  1. Staplee Feature: The feature which will be stapled to an existing site definition
  2. Stapler Feature: The feature which will staple the staplee feature with site definition

Steps

  1. Create new Empty SharePoint Project.
  2. Create staplee feature which we want to get activated on site provisioning.
  3. Create Stapler feature with scope as Farm.
  4. Add new Empy Element "StaplerElementHelper" to the project.
  5. Edit Element.xml file
  6. Add follwing line<FeatureSiteTemplateAssociation Id="d3f566c2-b711-4ddb-808b-c4d0c00dac36" TemplateName="STS#0" />FeatureSiteTemplateAssociation is going to associate our steplee feature with site definition "STS#0"(team site).
    Note: Replace value of Id with your staplee feature ID. You can also specify your own TemplateName.

    your complete file will look like
    <?xml version="1.0" encoding="utf-8"?>
    <Elements xmlns="http://schemas.microsoft.com/sharepoint/"><FeatureSiteTemplateAssociation Id="d3f566c2-b711-4ddb-808b-c4d0c00dac36" TemplateName="STS#0" />
    </Elements>
  7. Double click your Stapler Feature and Include "StaplerElementHelper" in "Items in feature". Remember it should be included in Stapler feature and not in staplee feature.
  8. Deploy it.



Now whenever you create a new site using site definition specified in FeatureSiteTemplateAssociation staplee feature will automatically get activated.

Sample Project

Popular Posts