March 14, 2012

Copying a folder to a location as a Feature

Here, my requirement is to copy an .asmx file to IIS root folder like [C:\inetpub\wwwroot\wss\VirtualDirectories\\]....It gets copied to the location on activation and deletes the folder on deactivation.

-- Add a feature to the solution and add an event receiver to the feature with the following code below:
-----------------------------
       string strport;
        string loc = @"C:\inetpub\wwwroot\wss\VirtualDirectories\";
        public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {
            ///// Code for copying (service) folder to IIS root folder.
            string strfileloc;
           SPWeb objSPWeb = (properties.Feature.Parent as SPSite).OpenWeb();
            strport = objSPWeb.Site.Port.ToString();
          
            strfileloc = loc + strport;
            bool exists = System.IO.File.Exists(strfileloc + @"\service\GetPreferredContent.asmx");
            if (!exists)
            {
                CopyFolder(@"C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\TEMPLATE\FEATURES\Wipro.MyBrand.ServiceFileCopy_ServiceFolderCopying\service\", strfileloc + @"\service");
            }
            ///// Code for copying (service) folder to IIS root folder.///////
        }

static public void CopyFolder(string sourceFolder, string destFolder)
        {
            if (!Directory.Exists(destFolder))
                Directory.CreateDirectory(destFolder);
            string[] files = Directory.GetFiles(sourceFolder);
            foreach (string file in files)
            {
                string name = Path.GetFileName(file);
                string dest = Path.Combine(destFolder, name);
                File.Copy(file, dest);
            }
            string[] folders = Directory.GetDirectories(sourceFolder);
            foreach (string folder in folders)
            {
                string name = Path.GetFileName(folder);
                string dest = Path.Combine(destFolder, name);
                CopyFolder(folder, dest);
            }
        }

public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
        {
            string strfileloc;
            SPWeb objSPWeb = (properties.Feature.Parent as SPSite).OpenWeb();
            strport = objSPWeb.Site.Port.ToString(); //sps.Port.ToString();
           strfileloc = loc + strport;
            bool exists = System.IO.File.Exists(strfileloc + @"\service\GetPreferredContent.asmx");
            if (exists)
            {
                File.Delete(strfileloc + @"\service\GetPreferredContent.asmx"); // For Deleting the File from the Folder
                Directory.Delete(strfileloc + @"\service"); // For Deleting the Folder
            }
        }



No comments:

Migrate SharePoint 2010 Infopath to SharePoint Online

 Below are the steps to follow to migrate the Infopath data to SP Online.