Add a custom section to web.config

If you have created a custom section like this, and if you want to add it programmatically to web.config then you have to:

  1. Use the WebConfigurationManager class and open the web

Configuration config = WebConfigurationManager.OpenWebConfiguration(path, site);

From MSDN

path

The virtual path to the configuration file.

site

The name of the application Web site, as displayed in IIS configuration.

 

  1. Create a ConfigurationSectionGroup and add it to the Configuration object

ConfigurationSectionGroup sectionGroup = new ConfigurationSectionGroup();

config.SectionGroups.Add(“GROUP_NAME”, sectionGroup);

 

  1. Create a new instance of your section (MyCustomSection) and add it to the section group

MyCustomSection section = new MyCustomSection ();

sectionGroup.Sections.Add(“SECTION_NAME”, section);

 

  1. Fill your custom section properties

section.Property1 = “0123456789”;

section.Property2 = “Lorem ipsum dolor sit amet”;

 

  1. And save the configuration

config.Save();

If you take a look at your web.config you will see that it was modified programmatically as you specified. I use this technique in the setup process of sites. Cool!

How to upload a list template

public static SPFile UploadListTemplate(string siteUrl, string templatePath)

{

using (SPSite site = new SPSite(siteUrl))

{

SPDocumentLibrary templates = (SPDocumentLibrary)site.GetCatalog(SPListTemplateType.
ListTemplateCatalog);

 

System.IO.FileInfo fileInfo = new System.IO.FileInfo(templatePath);

byte[] bytes = System.IO.File.ReadAllBytes(fileInfo.FullName);

 

return templates.RootFolder.Files.Add(fileInfo.Name, bytes);

}

}

How to upload a site template

public static SPFile UploadWebTemplate(string siteUrl, string templatePath)

{

using (SPSite site = new SPSite(siteUrl))

{

SPDocumentLibrary templates = (SPDocumentLibrary)site.GetCatalog(SPListTemplateType.WebTemplateCatalog);

 

System.IO.FileInfo fileInfo = new System.IO.FileInfo(templatePath);

byte[] bytes = System.IO.File.ReadAllBytes(fileInfo.FullName);

 

return templates.RootFolder.Files.Add(fileInfo.Name, bytes);

}

}

How to create a custom section for an application config file

First we need to create a custom ConfigurationElement. This configuration element only has a value attribute.

public class MyConfigurationElement : ConfigurationElement

{

/// <summary>

/// Initializes a new instance of the <see cref="ValueConfigurationElement"/> class.

/// </summary>

public MyConfigurationElement ()

{}

 

/// <summary>

/// Gets or sets the value.

/// </summary>

/// <value>The value.</value>

[ConfigurationProperty("value", IsRequired = true)]

public string Value

{

get { return this["value"] as string; }

set { this["value"] = value as string; }

}

}

 

Second we need to create the custom ConfigurationSection :

public class MyCustomSection : ConfigurationSection

{

/// <summary>

/// Gets or sets the property 1.

/// </summary>

/// <value>The property 1.</value>

[ConfigurationProperty("Property1", IsRequired = true)]

public MyConfigurationElement Property1

{

get { return (MyConfigurationElement)this["Property1"]; }

set { this["Property1"] = value; }

}

 

/// <summary>

/// Gets or sets the Property 2.

/// </summary>

/// <value>The Property 2.</value>

[ConfigurationProperty("Property2", IsRequired = true)]

public MyConfigurationElement Property2

{

get { return (MyConfigurationElement)this["Property2"]; }

set { this["Property2"] = value; }

}

 

}

 

This custom section has 2 MyConfigurationElements: Property1 and Property2

And finally, the application config

<sectionGroup name="CustomGroup" type="System.Configuration.ConfigurationSectionGroup, System.Configuration, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" >

<section name="CustomSection" type=" MyCustomSection, Version=1.0.0.0, Culture=neutral " />

</sectionGroup>

 

<CustomGroup>

<CustomSection>

<Property1 value="0123456789" />

<Property2 value="Lorem ipsum dolor sit amet" />

</CustomSection>

</CustomGroup>

 

See here how you can add this section programmatically to your web.config

How to create an item in a discussion list

Creating a discussion in discussion list is different from creating a simple list item. The code for creating a discussion item is:

using (SPSite site = new SPSite(“SITE URL”))

{

using (SPWeb web = site.OpenWeb())

{

SPList list =web.Lists[“LIST TITLE”];

result = SPUtility.CreateNewDiscussion(list.Items, “SUBJECT”);

result[SPBuiltInFieldId.Body] = “BODY”;

result.Update();

}

}

ItemUpdating, AfterProperties and field names

I was checking for a field value change in a WSS V3 list with the help of event handlers with no success.

In the ItemUpdating event, the list item has the current values of the item and the AfterProperties has the values that the item will have after the update

The problem was that I was using the display name of the field rather than the internal name. In the SPListItem you can use the display name but also the internal name, but with the AfterProperties you can't use the display name. You must use the internal name of the field.