Ads

Tuesday, 2 April 2013

See Real error in SharePoint by modifying web.config file

Showing the real error message in SharePoint
There are two settings in your web.config you need to change to enable custom errors.
This web.config is located in c:\inetpub\wwwroot\VirtualDirectories. In that folder you have 2 possible folder names: folder names with a number and folder names with a name. If you are using hostnames then the web.config file you are looking for is contains in the folder. If you are not using hostnames, the name of the folder corresponds to the port your web application is running on.

Open the web.config and look for the CallStack=”false” attribute. Put it to true.
Then search for the CustomErrors=”on” tag and change it to off.

Once those two actions are done you will see a more detailed error message.

Programatically Modifying-Web-Config-File : C# to Change web.config
Sometimes as per requirement, we need to add third party DLL's to our SharePoint solution in order to achieve some functionality that is not available in SharePoint. But in order to use this third party DLL in our application, we sometimes need to follow the below outlined steps.
In order to place the DLL in the bin folder of the sharepoint site, we would have to modify the Trust Level attribute from WSS_Minimal to Full in the web config file of the SharePoint site.

Here is the code snipet to make the above mentioned changes in the web config file programmatically. For this example, I am placing the code snipet in the feature activated event of a Feature present in the solution, having scope of web application.
// Gets the current webapplication from feature properties.                        SPWebApplication webApp = properties.Feature.Parent as SPWebApplication 
   // Declairs the webConfigmodification variable.
   SPWebConfigModification myModification = newSPWebConfigModification("level", "configuration/system.web/trust");
   //Gets a collection of web config modification.
   System.Collections.ObjectModel.Collection<SPWebConfigModification> allModifications = webApp.WebConfigModifications;

         myModification.Value = "Full";
         myModification.Owner = "OwnerName";
         myModification.Sequence = 1;    
         myModification.Type = SPWebConfigModification.SPWebConfigModificationType.EnsureAttribute; 
         allModifications.Add(myModification); // Add the modifications.

   SPFarm.Local.Services.GetValue<SPWebService>().ApplyWebConfigModifications();
   webApp.Update(); // Update the Web application.


Removing-web-config-modifications-made-by-user: C# to Change web.config

Sometimes it is required to make modifications in the web config file of a sharepoint site in order to achieve some functionality. For doing this it is suggested to place the code spinet in the feature activated event of a feature, so that whenever activating that feature in the sharepoint site the required changes in the web config file are made automatically.
To know more on how to modify web config file, please refer C# to Change web.config
Its very important from the security point of view to remove all the modifications made by a user while deactivating the feature. The below code snipet will help you to achieve this. Put these code snipet in the feature deactivating event of the same feature which made those modifications.
// Get the current web application from featutre property.
SPWebApplication webApp = properties.Feature.Parent asSPWebApplication;
// Declairs the webConfigmodification variable.
Collection<SPWebConfigModification> modificationCollection = webApp.WebConfigModifications;
// Declairs the webConfigmodification variable.
Collection<SPWebConfigModification> removeCollection = newCollection<SPWebConfigModification>();
int count = modificationCollection.Count;

for (int i = 0; i < count; i++)
   {
      SPWebConfigModification modification = modificationCollection[i];
         // check and get all the recent modifications made by "TwitterWebPart" owner.
         if (modification.Owner == "OwnerName")
                  {
                        removeCollection.Add(modification); // collect modifications to delete
                  }
   }
// now delete the modifications from the web application
if (removeCollection.Count > 0)
         {
            foreach (SPWebConfigModification modificationItem in removeCollection)
               {                .
                   webApp.WebConfigModifications.Remove(modificationItem);  //remove the modifications from web config
               }
        }
SPFarm.Local.Services.GetValue<SPWebService>().ApplyWebConfigModifications();
webApp.Update();

No comments:

Post a Comment

Ads