Tuesday, December 10, 2013

Hide sharepoint webparts (Server side code and client side code)

There are two ways to create sharepoint web parts. once is visual webpart another one is normal web part.
Since we all know the difference between those two , there are no point of covering about that here.
But the question is, how can we hide entire web part when the time comes? There are two ways to hide it. one is client side scripting another one is server side coding.

Client side script to hide (Javascript)

Hide the webpart which is create as normal web part in sharepoint

HTML Content of that web part.

      <div id=""div1"">
                    <div id=""div2"" class = ""user-profile-wp-header"">
                       
                    </div>
                    <div id=""div3"">
                        <div id='div4' class=""recom-nav-next"">
                               
                        </div>                                                                 
                    </div>
                    <div id=""div5"">
                   
                    </div>
      </div>

This web part can be hide using javascript. get the outerwrap div id and use it to hide that webpart, or else get the webpart ID using sharepoint designer then use that id instead of outer div Id.

You can use this javascript to hide,
                 $('#div1').hide();

Server side code (C#)

You can hide the entire web part in page load method ( or can be anywhere) if it is a visual web part.

protected void Page_Load(object sender, EventArgs e)
{
                this.ChromeType = PartChromeType.None;
                this.Hidden = true;  
}


Update app config values in run time using C#

Normally we use app.config file to store some config values. as we all know, we can easily read values from app.config using C#. but we rarely update that file in run time. but this is also possible in C#.
here i have explained how to write back(Add new keys or update values of certain key) to app.config file in run time.


/// <summary>
/// Update app config values
/// </summary>
 /// <param name="key">app config key</param>
/// <param name="value">app config value</param>

public static void UpdateAppSettings(string key, string value)
{
          // Give the correct path of an app.config file
          Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            if (ConfigurationManager.AppSettings[key] == null)
            {
                config.AppSettings.Settings.Add(key, value);
            }
            else
            {
                config.AppSettings.Settings[key].Value = value;
             
            }
            config.Save(ConfigurationSaveMode.Modified);
            ConfigurationManager.RefreshSection("appSettings");          
}

Write a main method and call this function to update app.config values. cool :)


Store and Read Cookies using Javascript

When we are working in client side coding (Javascript or normal sharepoint web part projects), we cannot use session variables for storing values. so in that case, we go with cookies. here i have mentioned how to read and store cooikes using javascript.

we can also set the expire time for cookies (in ms).

// expire time = 2 min (120*1000 ms)
storeCookie = function(CookieName,value)
{
    var exdate = new Date();
    var time = exdate.getTime();
    time += 120 * 1000;
    exdate.setTime(time);
    document.cookie = CookieName+ "=" + value + '; expires=' + exdate.toGMTString() ;

};


readCookie = function(CookieName)
{
    var value = document.cookie;
    var start = value.indexOf(" " + CookieName+ "=");
    if (start == -1)
    {
        start = value.indexOf(CookieName+ "=");
    }
    if (start == -1)
    {
        value = null;
    }
    else
    {
        start = value.indexOf("=", start) + 1;
        var end = c_value.indexOf(";", start);
        if (end == -1)
        {
            end = value.length;
        }
        value = unescape(value.substring(start,end));
    }
    return value;
};