Monday, June 10, 2013

Differences Between Sandboxed and Farm Solutions

Hi Guys,
In this post we are going to list out the differences between Sandboxed and Farm based Solutions.

Farm based Solutions:
Farm solutions are hosted in the IIS worker process (W3WP.exe).It runs the code that can affect the whole farm.So whenever you deploy a farm solution you must restart the application pool or ISS Server.
If you deploy any feature or retract any feature the whole application pool got recycled.
W3WP is the worker process to which you should attach while debugging Farm based solutions.

Sandboxed Solutions:
Sandboxed solutions are hosted in the SharePoint user code solution worker process (SPUCWorkerProcess.exe).It runs the code that can only affect the site collection of the solution.
Whenever you deploy a sandbox solution, no need of restarting neither the IIS application pool nor the IIS server as sandboxed solutions do not run in the IIS worker process.
SPUCWorkerProcess is the worker process to which you should attach while debugging sandbox solutions.

Points to be noted:
•One major difference is we can't create Aplication pages in Sandbox solutions.Beacuse Application pages are stored in the 14\TEMPLATES\_LAYOUTS and when we deploy as sandbox we dont have permissions to the physical folder.
•Also we cant create VISUAL web parts in Sandbox soultions.
•We cant use code to connect to the external web services or to database in the sandbox soltion
•Farm solutions are installed and deployed. Sandboxed solutions are uploaded and activated.
runwithelevatedprivileges doesn't work in Sandboxed solutions.
• Site collection adminstrator can deploy a Sandboxed solutions, where as Farm administrator can only deploy Farm based Solutions.

Hope this helps...

Wednesday, May 15, 2013

How to export DataTable to Excel using C#

In this post we will be exporting the DataTable to an Excel using C#
Below is the method which will do the trick. Just pass the parameters datatable and the filename.

private void ExportDataTableToExcel(DataTable thetable, string fileName)
        {
            string attachment = "attachment; filename=" + fileName + ".xls";

            Response.ClearContent();

            Response.AddHeader("content-disposition", attachment);

            Response.ContentType = "application/vnd.ms-excel";

            //write columns from DataTable to Excel      
            string tab = "";
            foreach (DataColumn dc in thetable.Columns)
            {
                string title = string.Empty;
                if (dc.ColumnName.Contains("Meta"))
                    title = "Metadata Properties";
                else
                    title = dc.ColumnName;
                Response.Write(tab + title);
                tab = "\t";
            }
            Response.Write("\n");

            //write rows from DataTable to Excel  
            int i;
            foreach (DataRow dr in thetable.Rows)
            {
                tab = "";
                for (i = 0; i < thetable.Columns.Count; i++)
                {
                    Response.Write(tab + dr[i].ToString());
                    tab = "\t";
                }
                Response.Write("\n");
            }
            //To throw the excel file generated
            Response.End();

        }


Hope this helps...

Deleting all versions of a document present in document library except last 2 major and 1 minor versions Moss2007/Sharepoint2010

Recently i came across a requirement to develop a admin component which will delete all the versions of the document except some major and minor verisons present in a Sharepoint document library. Thought of sharing with you guys aswell.
In this post we are going to Delete all versions of a document present in document library except last 2 major and 1 minor versions
Below is the code snippet which performs the action.
You can made the variables int majorVersionsRequired and int minorVersionsRequired are as configurable depending on the versions you require.

try
{
   using (SPSite site = new SPSite("<<http://siteurl/>>"))
   {
      using (SPWeb web = site.OpenWeb())
      {
         SPList list = web.Lists["<<Doc Lib Name>>"];
         int majorVersionsRequired = 2;
         int minorVersionsRequired = 1;

         foreach (SPListItem item in list.Items)
         {
            SPListItemVersionCollection versionCol = item.Versions;
            int count = versionCol.Count;
            int tempMajorVersionsRequired = 0;
            int tempMinorVersionsRequired = 0;
            int versionSiNo = 0;
                           
            for (int i = 0; i <= count - 1; i++)
            {
               if (versionCol[i - versionSiNo].Level == SPFileLevel.Published)
               {
                  // this is a major version
                  tempMajorVersionsRequired++;
                  if (tempMajorVersionsRequired > majorVersionsRequired)
                  {
                     SPListItemVersion version = versionCol[i - versionSiNo];
                     version.Delete();
                     versionSiNo++;
                  }
               }
               else if (versionCol[i - versionSiNo].Level == SPFileLevel.Draft)
               {
                  //this is a minor version
                  tempMinorVersionsRequired++;
                  if (tempMinorVersionsRequired > minorVersionsRequired)
                  {
                     SPListItemVersion version = versionCol[i - versionSiNo];
                     version.Delete();
                     versionSiNo++;
                  }
               }
            }
          } 
      }
   }
}
catch (Exception ex)
{

}


Hope this helps...

Popular Posts