Wednesday, March 28, 2012

Percentage Complete Progress Bar

Percentage Complete Progress Bar can be done by various way. Below is more flexible interface for progress bar. It's web control which you can add it in master page or any page.

using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Tittle
{
public class PercentageComplete : WebControl, INamingContainer
{
private string borderColor = "#547095";
private string borderType = "solid";
private string borderWidth = "1";
private string bgColor = "white";
private string controlWidth = "95%";
private string cellpadding = "0";
private string cellspacing = "0";
private string controlAlignment = "center";
private string fillColor = "#FF7979";
private string startColor = "#E5E5E5";
private string endColor = "#FF7979";
private bool gradientVertical = false;
private string textFontSize = "1";
private string textFontFamily = "verdana";
private string textFontColor = "#3D526D";
private bool textBold = false;
private decimal count = 0;
private string text = "";
private bool displayText = true;
private bool gradient = true;
private string remainingColor = "";

/// < summary>
/// Integer or decimal value, mandatory, which tells how much percentage is completed.
/// < /summary>
public decimal Count { set { count = value; } get { return count; } }
/// < summary>
/// You can override default percentage text with this.. e.g. if 19% is completed, then
/// it will show "19%" text in middle, but you change text to "2 of 10" something like this.
/// < /summary>
public string Text { set { text = value; } get { return text; } }
/// < summary>
/// By default it is true, set to false, and no text will be displayed in middle.
/// < /summary>
public bool DisplayText { set { displayText = value; } get { return displayText; } }
/// < summary>
/// Box Border Color, this can be overwritten by Remaining Color.
/// < /summary>
public string BgColor { set { bgColor = value; } get { return bgColor; } }
/// < summary>
/// Box Width (default 100%)
/// < /summary>
public string ControlWidth { set { controlWidth = value; } get { return controlWidth; } }
/// < summary>
/// Cellpadding of Table. default 0.
/// < /summary>
public string Cellpadding { set { cellpadding = value; } get { return cellpadding; } }
/// < summary>
/// Cellspacing of Table. default 0.
/// < /summary>
public string Cellspacing { set { cellspacing = value; } get { return cellspacing; } }
/// < summary>
/// Box Alignment in the container, by default center
/// < /summary>
public string ControlAlignment { set { controlAlignment = value; } get { return controlAlignment; } }
/// < summary>
/// Box Border Color (E.g. red,#c0c0c0)
/// < /summary>
public string BoxBorderColor { set { borderColor = value; } get { return borderColor; } }
/// < summary>
/// Box Border Type i.e. (solid, dashed, dotted, ridge, inset, outset)
/// < /summary>
public string BorderType { set { borderType = value; } get { return borderType; } }
/// < summary>
/// Box Border Width i.e. (0, 1, 2.. )
/// < /summary>
public string BoxBorderWidth { set { borderWidth = value; } get { return borderWidth; } }
/// < summary>
/// Percentage Completed Color.
/// < /summary>
public string FillColor { set { fillColor = value; } get { return fillColor; } }
/// < summary>
/// If percentage complete is 60%, then remaining 40% will have this color in background, no gradient supported for remaining color.
/// < /summary>
public string RemainingColor { set { remainingColor = value; } get { return remainingColor; } }
/// < summary>
/// By default gradient effect is true, set this to false, and fill color will take preference over startcolor and endcolor
/// < /summary>
public bool Gradient { set { gradient = value; } get { return gradient; } }
/// < summary>
/// By default gradient effect, and this is start color of gradient effect.
/// < /summary>
public string StartColor { set { startColor = value; } get { return startColor; } }
/// < summary>
/// By default gradient effect, and this is end color of gradient effect.
/// < /summary>
public string EndColor { set { endColor = value; } get { return endColor; } }
/// < summary>
/// By default gradient horizontal is true, you can set to to vertical instead of horizontal (default is false)
/// < /summary>
public bool GradientVertical { set { gradientVertical = value; } get { return gradientVertical; } }
/// < summary>
/// Font Size of Text displayed in middle.
/// < /summary>
public string TextFontSize { set { textFontSize = value; } get { return textFontSize; } }
/// < summary>
/// Font family/face of text displayed in middle.
/// < /summary>
public string TextFontFamily { set { textFontFamily = value; } get { return textFontFamily; } }
/// < summary>
/// Font color of text displayed in middle.
/// < /summary>
public string TextFontColor { set { textFontColor = value; } get { return textFontColor; } }
/// < summary>
/// Whether displayed text in middle should be bold or not. (default false)
/// < /summary>
public bool TextBold { set { textBold = value; } get { return textBold; } }


public PercentageComplete() : base()
{

}

protected override void Render(HtmlTextWriter output)
{
base.Render(output);
string s = " ";
s += "< table cellspacing='" + cellspacing + "' cellpadding='" + cellpadding + "' style='border:" + borderWidth + " " + borderType + " " + borderColor + ";background-color:" + bgColor +"' width='" + controlWidth + "' align='" + controlAlignment + "' >";
s += " < tr >";
s += " < td width='100%' style='background-color:"+remainingColor+"' >";
s += " < div style='position:absolute;background-color:" + fillColor + ";width:" + count + "%;";
if ( gradient == true )
{
s += " filter:progid:DXImageTransform.Microsoft.Gradient(GradientType=" + (gradientVertical==true?"1":"0") + ",StartColorStr=" + startColor + ", EndColorStr=" + endColor + ")";
}
s += " ;' align='center'>< font size='" + TextFontSize + "'> < /font>< /div> ";
string textPart=" ";
if ( displayText == true )
{
if ( text == "" )
textPart = count.ToString() + "%";
else
textPart = text;
}

s += " < div style='position:relative;width:100%' align='center' >< font " + (textBold==true?"style='font-weight:bold'":"") + " size='" + textFontSize + "' color='" + textFontColor + "' face='" + textFontFamily + "' >" + textPart + "< /font>< /div>";
s += " < /td>";
s += "< /tr>";
s += "< /table>";
output.Write(s);
}
}
}





how to use


< Controls:PercentageComplete id="PercentageComplete2" runat="server" Count="65" FillColor="#0099FF" Gradient="false" cellspacing="2" TextFontColor="#FF6600" TextBold="true" TextFontSize="2" RemainingColor="#ABD0BC" />
< Controls:PercentageComplete id="PercentageComplete1" runat="server" Count="79" Text="79% occupied" />
< Controls:PercentageComplete id="Percentagecomplete4" runat="server" Count="80" Text="8 of 10" StartColor="#6868B9" EndColor="#ABD0BC" BoxBorderColor="#FF6600" CellSpacing="0" TextFontFamily="Times New Roman" TextFontColor="White" TextFontSize="3" BorderType="double" BoxBorderWidth="3" />
< Controls:PercentageComplete id="Percentagecomplete3" runat="server" Count="55" StartColor="#B7B748" EndColor="#E5E5E5" BoxBorderColor="green" BgColor="#E5E5E5" BorderType="dotted" />
< Controls:PercentageComplete id="Percentagecomplete5" runat="server" Count="90" StartColor="#FF6600" EndColor="#0099FF" BoxBorderColor="Red" CellPadding="3" cellspacing="1" TextFontFamily="Verdana" TextBold="True" TextFontColor="#FF9999" TextFontSize="3" BorderType="inset" GradientVertical="True" BoxBorderWidth="1" />
< Controls:PercentageComplete id="Percentagecomplete6" runat="server" Count="45" StartColor="#99FF00" EndColor="#6600FF" BoxBorderColor="Red" CellPadding="4" BgColor="#E8E8FF" BorderType="dashed" TextFontFamily="Tahoma" TextFontColor="red" TextFontSize="2" />

Saturday, March 10, 2012

SharePoint - Feature Activation Tool

Here is sample code which will activate/deactivate site/web feature for whole web application

Configuration:


    < add key="WebApplicationName" value="{Web Application URL}" />
    < add key="DeploymentType" value="Deploy {or Rollback}" />
    < add key="FeatureID" value="{Feature Id}" />
    < add key="FeatureType" value="web{or Site}" />


Code:


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using Microsoft.SharePoint;
using System.Configuration;
using Microsoft.SharePoint.Administration;

namespace FeatureActivation
{
    class Program
    {
        private static StreamWriter logWriter;
        private static Guid FeatureGUID;

        static void Main(string[] args)
        {
            logWriter = new StreamWriter("FeatureActivationLog.txt");
            string strWebApplicationName = Convert.ToString(System.Configuration.ConfigurationManager.AppSettings["WebApplicationName"]);
            FeatureGUID = new Guid(Convert.ToString(ConfigurationManager.AppSettings["FeatureID"]));
            string strDeploymentType = Convert.ToString(ConfigurationManager.AppSettings["DeploymentType"]);
            string strFeatureType = Convert.ToString(ConfigurationManager.AppSettings["FeatureType"]);
            int siteCollectionCount = 0;
            int siteCount = 0;
            int webCount = 0;
            DateTime startTime;
            DateTime endTime;
            if (string.IsNullOrEmpty(strWebApplicationName))
            {
                Console.WriteLine("Configuration Error");
                Console.WriteLine("Press enter to continue...");
                Console.ReadLine();
            }
            else
            {
                SPSecurity.RunWithElevatedPrivileges(delegate()
                {

                    if (strDeploymentType.ToLower() == "deploy")
                    {
                        if (!IsFeatureInstall())
                        {
                            Console.WriteLine("Feature is not installed");
                            logWriter.WriteLine("Feature is not installed");
                            logWriter.WriteLine("==========================================================================");
                            return;
                        }
                    }
                    if (strFeatureType.ToLower() == "site")
                    {
                        using (SPSite objSite = new SPSite(strWebApplicationName))
                        {
                            SPSiteCollection objSiteCollection = objSite.WebApplication.Sites;
                            if (objSiteCollection != null && objSiteCollection.Count > 0)
                            {
                                startTime = DateTime.Now;
                                foreach (SPSite objTempSite in objSiteCollection)
                                {
                                    siteCollectionCount++;
                                    try
                                    {
                                        if (strDeploymentType.ToLower() == "deploy")
                                        {
                                           
                                                ActivateSiteFeature(objTempSite);
                                                Console.WriteLine("Activate feature for " + objTempSite.Url);
                                                logWriter.WriteLine("Activate feature for " + objTempSite.Url);
                                                logWriter.WriteLine("==========================================================================");
                                           
                                        }
                                        else if (strDeploymentType.ToLower() == "rollback")
                                        {
                                           

                                                DeactivateSiteFeature(objTempSite);
                                                Console.WriteLine("Deactivate feature for " + objTempSite.Url);
                                                logWriter.WriteLine("Deactivate feature for " + objTempSite.Url);
                                                logWriter.WriteLine("==========================================================================");


                                           
                                        }
                                        else
                                        {
                                            logWriter.WriteLine("Unknown Deployment Type");
                                            logWriter.WriteLine("==========================================================================");
                                            Console.WriteLine("Unknown Deployment Type");
                                        }

                                    }
                                    catch (Exception ex)
                                    {
                                        Console.WriteLine("Orphaned site or could be any other reason. Please check log for more detail: " + ex.Message + ex.StackTrace); ;
                                        logWriter.WriteLine("Orphaned site or could be any other reason. Please check log for more detail: " + ex.Message + ex.StackTrace);
                                        logWriter.WriteLine("==========================================================================");
                                        continue;
                                    }


                                    objTempSite.Close();
                                }
                                endTime = DateTime.Now;
                                logWriter.WriteLine("**********************SUMMARY*****************************");
                                Console.WriteLine("Total Number Of SiteCollection for " + strWebApplicationName + " is " + siteCollectionCount);
                                logWriter.WriteLine("Total Number Of SiteCollection for " + strWebApplicationName + " is " + siteCollectionCount);

                                Console.WriteLine("Start Time Of " + strWebApplicationName + " is " + startTime.ToString());
                                logWriter.WriteLine("Start Time Of " + strWebApplicationName + " is " + startTime.ToString());

                                Console.WriteLine("End Time Of " + strWebApplicationName + " is " + endTime.ToString());
                                logWriter.WriteLine("End Time Of " + strWebApplicationName + " is " + endTime.ToString());
                                logWriter.Close();
                            }
                        }
                    }
                    else if (strFeatureType.ToLower() == "web")
                    {
                        using (SPSite objSite = new SPSite(strWebApplicationName))
                        {
                            SPSiteCollection objSiteCollection = objSite.WebApplication.Sites;
                            if (objSiteCollection != null && objSiteCollection.Count > 0)
                            {
                                startTime = DateTime.Now;
                                foreach (SPSite objTempSite in objSiteCollection)
                                {
                                    try
                                    {
                                        siteCollectionCount++;
                                        foreach (SPWeb objTempWeb in objTempSite.AllWebs)
                                        {
                                            webCount++;
                                            try
                                            {
                                                if (strDeploymentType.ToLower() == "deploy")
                                                {
                                                    ActivateWebFeature(objTempWeb);
                                                    Console.WriteLine("Activate feature for " + objTempWeb.Url);
                                                    logWriter.WriteLine("Activate feature for " + objTempWeb.Url);
                                                    logWriter.WriteLine("==========================================================================");
                                                }
                                                else if (strDeploymentType.ToLower() == "rollback")
                                                {
                                                    DeactivateWebFeature(objTempWeb);
                                                    Console.WriteLine("Deactivate feature for " + objTempWeb.Url);
                                                    logWriter.WriteLine("Deactivate feature for " + objTempWeb.Url);
                                                    logWriter.WriteLine("==========================================================================");

                                                }
                                                else
                                                {
                                                    logWriter.WriteLine("Unknown Deployment Type");
                                                    logWriter.WriteLine("==========================================================================");
                                                    Console.WriteLine("Unknown Deployment Type");
                                                }

                                            }

                                            catch (Exception ex)
                                            {
                                                // for sub sites.
                                                Console.WriteLine("Orphaned site or could be any other reason. Please check log for more detail: " + ex.Message + ex.StackTrace);
                                                logWriter.WriteLine("Orphaned site or could be any other reason. Please check log for more detail: " + ex.Message + ex.StackTrace);
                                                logWriter.WriteLine("==========================================================================");
                                                continue;
                                            }
                                            objTempWeb.Close();
                                        }
                                    }
                                    catch (Exception ex)
                                    {
                                        // for site collection.
                                        Console.WriteLine("Orphaned site or could be any other reason. Please check log for more detail: " + ex.Message + ex.StackTrace);
                                        logWriter.WriteLine("Orphaned site or could be any other reason. Please check log for more detail: " + ex.Message + ex.StackTrace);
                                        logWriter.WriteLine("==========================================================================");
                                        continue;
                                    }

                                    objTempSite.Close();
                                }
                                endTime = DateTime.Now;

                                logWriter.WriteLine("**********************SUMMARY*****************************");
                                Console.WriteLine("Total Number Of SiteCollection for " + strWebApplicationName + " is " + siteCollectionCount);
                                logWriter.WriteLine("Total Number Of SiteCollection for " + strWebApplicationName + " is " + siteCollectionCount);

                                Console.WriteLine("Total Number Of Web for " + strWebApplicationName + " is " + webCount);
                                logWriter.WriteLine("Total Number Of Web for " + strWebApplicationName + " is " + webCount);

                                Console.WriteLine("Start Time Of " + strWebApplicationName + " is " + startTime.ToString());
                                logWriter.WriteLine("Start Time Of " + strWebApplicationName + " is " + startTime.ToString());

                                Console.WriteLine("End Time Of " + strWebApplicationName + " is " + endTime.ToString());
                                logWriter.WriteLine("End Time Of " + strWebApplicationName + " is " + endTime.ToString());
                                logWriter.Close();
                            }
                        }
                    }
                    else
                    {
                        logWriter.WriteLine("Unknown Feature Type");
                        logWriter.WriteLine("==========================================================================");
                        Console.WriteLine("Unknown Feature Type");
                    }
                });
                Console.WriteLine("Press enter to continue...");
                Console.ReadLine();
            }
        }

        private static bool IsFeatureInstall()
        {
            bool bFound = false;
            //Finds the feature definition.
            foreach (SPFeatureDefinition featureDef in SPFarm.Local.FeatureDefinitions)
            {
                try
                {
                    if (featureDef.Id.Equals(FeatureGUID))
                    {
                        bFound = true;
                        break;
                    }
                }
                catch (Exception ex)
                {
                    //This is failing if feature is installed and feature file is not there
                    //we have to ignore this exception
                }
            }
            return bFound;
        }

        private static void ActivateSiteFeature(SPSite objSite)
        {
            bool bActive = false;
            //Checks whether the user is activated already.
            foreach (SPFeature feature in objSite.Features)
            {
                if (feature.DefinitionId.Equals(FeatureGUID))
                {
                    bActive = true;
                    break;
                }
            }

            if (!bActive)
            {
                objSite.Features.Add(FeatureGUID);
            }
        }
        private static void DeactivateSiteFeature(SPSite objSite)
        {
            bool bActive = false;
            //Checks whether the user is activated already.
            foreach (SPFeature feature in objSite.Features)
            {
                if (feature.DefinitionId.Equals(FeatureGUID))
                {
                    bActive = true;
                    break;
                }
            }

            if (bActive)
            {
                objSite.Features.Remove(FeatureGUID);
            }
        }
        private static void ActivateWebFeature(SPWeb objWeb)
        {
            bool bActive = false;
            //Checks whether the user is activated already.
            foreach (SPFeature feature in objWeb.Features)
            {
                if (feature.DefinitionId.Equals(FeatureGUID))
                {
                    bActive = true;
                    break;
                }
            }

            if (!bActive)
            {
                objWeb.Features.Add(FeatureGUID);
            }
        }
        private static void DeactivateWebFeature(SPWeb objWeb)
        {
            bool bActive = false;
            //Checks whether the user is activated already.
            foreach (SPFeature feature in objWeb.Features)
            {
                if (feature.DefinitionId.Equals(FeatureGUID))
                {
                    bActive = true;
                    break;
                }
            }

            if (bActive)
            {
                objWeb.Features.Remove(FeatureGUID);
            }
        }

    }
}

Tuesday, March 6, 2012

Capture click event of OOB control’s “save and close” link for Profile Page

Edit Profile page is OOB page and it has OOB control to edit profile.

< SPSWC:ProfileEditor ID="ProfileEditor" runat="server" oncontextmenu="return false" />

we can capture click event of "save and close" link of this control as given below

< script language="javascript">
$('a[id$="ProfileSave_i"]').bind('click', function() {
alert("img");
});
$('a[id$="ProfileSave"]').bind('click', function() {
alert("text");
});