Ads

Showing posts with label Web Config. Show all posts
Showing posts with label Web Config. Show all posts

Monday, 8 July 2013

Way of Safe Control Entries in Web Config File from code

There may come a situation where we want to add a custom new safe control entry in web.config file. 

Create New Empty SharePoint Project.

Add new "Empty Element" in project.

Select the newly created element and press F4 to open properties window.

Click ellipse button against the "Safe Control Entries" property as shown in below image.


The safe control entry dialog opens as shown below.

Click "Add" This will add a new safe control entry. Set properties 
Name: name of safe control entry
Assembly: A strong assembly name, such as: MyFirstProject.MyFirstAssembly, Version=1.0.0.0, Culture=neutral, PublicKeyToken=11e9bce111e9418c.
NameSpace: The fully-qualified namespace for the class/control
Safe: True

Add as many entries as you like.

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();

Saturday, 23 March 2013

C# code to do modifications in Sharepoint Web.Config file

As a developer, Many time requirements come to change the web cofig file of sharepoint.
We can achve this by writing some C# codes.
 
Modifying Web Config File:
Lets assume, we are integrating third party DLL in our application, So we need to place that dll file in bin folder. along with that we also need to modify the trust level attribute from WSS_Minimal to Full in the web config file of sharepoint.
I recommend to write in feature activation. 
//Gets the current web application 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

We also need to remove all the modifications while deactivating the feature. 
I recommend to write in 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)
      {                .
         //remove the modifications from web config
         webApp.WebConfigModifications.Remove(modificationItem);   
        
      }
   }
   SPFarm.Local.Services.GetValue<SPWebService>().ApplyWebConfigModifications();
   webApp.Update();





Ads