Thursday 24 April 2014

Unable to find .dll in GAC after deployment in Sharepoint 2013.

I thought of posting this solution in my blog because many of them might have faced the similar issue.

I was trying to deploy my project, but could not find an assembly in GAC even after successful deployment. I tried draggng the dll to "c:\windows\assembly" folder   and also tried installing that using "gacutil" , but I was not successful, finally I got to know the proper path of my dll where it was deployed.

Solution:

c:\windows\assembly, is the directory for the GAC in .NET versions 1.0 through 3.5. It has been moved to c:\windows\microsoft.net\assembly in .NET 4.0 and up. You can browse that directory with Explorer, the shell extension handler that flattened the view of the GAC directories is no longer used. If the assembly doesn't contain any unmanaged code then start in the GAC_MSIL directory, you'll have few problems locating the actual file from there.

Modify SharePoint web.config using object model....

Root Level Web.Config

  <appSettings>
    <
add key="AppKey" value="AppValue"/>  </appSettings>

Code Snippet

using System;using System.Configuration;using System.Web.Configuration;
namespace SampleApplication.WebConfig
{
    public partial class webConfigFile : System.Web.UI.Page    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //Helps to open the Root level web.config file.            Configuration webConfigApp = WebConfigurationManager.OpenWebConfiguration("~");
            //Modifying the AppKey from AppValue to AppValue1            webConfigApp.AppSettings.Settings["AppKey"].Value = "AppValue1";
            //Save the Modified settings of AppSettings.            webConfigApp.Save();        }
    }
}

Output of Root Level Web.Config:

 <appSettings>
    <
add key="AppKey" value="AppValue1"/>  </appSettings>

In order to modify application specific web.config entry below is the code snippet:



using System;
using System.Reflection;
using Microsoft.SharePoint.Administration;
 
namespace SharePointLiveForMe.SharePoint.Administration
{
    /// <summary>
    /// This class allows to add or remove entries in web.config.
    /// </summary>
    public class WebConfigManagement
    {
        /// <summary>
        /// This methods allows to add a key/value pair in the AppSettings section.
        /// </summary>
        /// <param name="Key">Name of the key</param>
        /// <param name="Value">Value</param>
        /// <param name="webApp">Web application</param>
        public static void AddAppSettingsKeyValue(string key, string value, SPWebApplication webApp)
        {
            if (!string.IsNullOrEmpty(key) && !string.IsNullOrEmpty(value) && webApp != null)
            {
                SPWebConfigModification modification = GetModification(key, value);
                //Add app setting key value if it is not there in web.config file 
                if (!webApp.WebConfigModifications.Contains(modification))
                {
                    webApp.WebConfigModifications.Add(modification);
                    webApp.Farm.Services.GetValue<SPWebService>().ApplyWebConfigModifications();
                    webApp.Update();
                }
                else
                {
                    //modify existing entry
                    webApp.WebConfigModifications.Add(modification);
                    webApp.Farm.Services.GetValue<SPWebService>().ApplyWebConfigModifications(); 
                    webApp.Update();
                } 
            }
            else
                throw new ArgumentNullException();
        }
 
        /// <summary>
        /// This methods allows to remove a key/value pair in the AppSettings section.
        /// </summary>
        /// <param name="key">Name of the key</param>
        /// <param name="value">Value</param>
        /// <param name="webApp">Web application</param>
        public static void RemoveAppSettingsKeyValue(string key, string value, SPWebApplication webApp)
        {
            if (!string.IsNullOrEmpty(key) && !string.IsNullOrEmpty(value) && webApp != null)
            {
                SPWebConfigModification modification = GetModification(key, value);
                if(webApp.WebConfigModifications.Contains(modification))
                {
                    webApp.WebConfigModifications.Remove(modification);
                    webApp.Farm.Services.GetValue<SPWebService>().ApplyWebConfigModifications();
                    webApp.Update();
                }
            }
            else
                throw new ArgumentNullException();
        }
 
        /// <summary>
        /// This method allows to build an SPWebConfigModification object.
        /// </summary>
        /// <param name="key">Name of the key</param>
        /// <param name="value">Value</param>
        /// <returns></returns>
        private static SPWebConfigModification GetModification(string key, string value)
        {
            if (!string.IsNullOrEmpty(key) && !string.IsNullOrEmpty(value))
            {
                SPWebConfigModification modification = new SPWebConfigModification();
                modification.Name = string.Format(@"add[@key=""{0}""]", key);
                modification.Path = "configuration/appSettings";
                modification.Value = string.Format(@"<add key=""{0}"" value=""{1}"" />", key, value);
                modification.Owner = Assembly.GetExecutingAssembly().FullName;
                modification.Sequence = 0;
                modification.Type = SPWebConfigModification.SPWebConfigModificationType.EnsureChildNode;
                return modification;
            }
            else
                throw new ArgumentNullException();
        }
    }
}

Wednesday 23 April 2014

Read sharepoint web.config using object model...


We need to initialize the necessary variables for ex as below

           //Initialize the variables
            string configFilePath = string.Empty;
            XmlDocument webConfigDoc = null;
            string appSettingVal = string.Empty;
            string xpath = string.Empty;

           configFilePath =  rootWeb.Site.WebApplication.GetIisSettingsWithFallback(Microsoft.SharePoint.Administration.SPUrlZone.Default).Path.FullName.ToString() + "\\web.config";
           webConfigDoc = new XmlDocument();
           webConfigDoc.Load(configFilePath);

           //Reading web.config attributes
           xpath = "configuration/appSettings/add[@key='" + <key value that you are looking for> + "']";
           XmlNode appSetting = webConfigDoc.SelectSingleNode(xpath);

            if (appSetting != null)
                    {
                       //gets the value for the key from appsettings section.
                        appSettingVal = appSetting.Attributes["value"].Value;
                    }

Thursday 10 April 2014

How to apply validations or cutomize OOB New forms, Edit forms, Disp Forms in Sharpoint 2010?



SharePoint List Form Validation with Jquery:


While we have our list opened in our browser we can use the List Tools to open the Default New Form. This is the form where we want to add our code

 
  
We are presented with a design view of the new form. We need to add a Content Editor Web Part so we have a space to enter our JavaScript code

 


Add Content Editor Web part


 





 

Add your JavaScript or Jquery Code inside Content Editor Web part





Now open the page  it will show the alert box