Showing posts with label C#. Show all posts
Showing posts with label C#. Show all posts

Friday, January 2, 2009

Programmatically adding web part to a SharePoint page from web part gallery

I had a requirement to add a web part to default.aspx page from Web part gallery. I have used a Feature to do the same. Please find the code below which is written in “Feature Activated” event.



Feature Activated




Add WebPart





Get WebPart XML



Hope the code is straightforward to understand.

Friday, December 5, 2008

Code to remove Web part from a page in SharePoint

Here the code snippet to remove Web Part from Web part zone of default.aspx page.

The SPLimitedWebPartManager provides a limited set of Web Part operations that can be performed in our object model.

Friday, September 19, 2008

Code snippet to get all installed languages from sharepoint server Farm


I had a requirement to display all installed languages of a SharePoint server farm in a DropDownList.

Regional Settings of SPWeb has a property named "SPWeb.RegionalSettings.InstalledLanguages", which returns language collection of type
"SPLanguageCollection". The following are the properties of "SPLanguage" instance.





Use the "Display Name" property to get the language display name ex. English and "LCID" property to get the locale ID ex. 1033(for English).
See the below code snippet, which will be used to get the installed languages from a SharePoint server farm.



Hope this will be helpful

Monday, January 7, 2008

Registering Assembly to GAC via Code

Registering Assembly to GAC via Code

As we know to register an assembly toGlobal Assembly Cache, the assembly must be strong named. This strong name is used to prevent spoofing of your code. Use the “gacutil.exe” to install the assembly to the GAC.


Example


gacutil /i MyAssembly.dll


The above command will installs the “MyAssembly.dll” in to the GAC.
See the following code snippet to do the above stuff by C# code.

try
{
string assemblyName = “MyAssembly.dll”;

ProcessStartInfo pStartInfo = new ProcessStartInfo();

//specify gacutil.exe whith which to start the process
pStartInfo.FileName = "gacutil.exe";
pStartInfo.Arguments = string.Format("/i {0}", assemblyName);
pStartInfo.UseShellExecute = false;

//start the process
Process process = Process.Start(pStartInfo);

//wait till the process completes
process.WaitForExit();
}
catch (Exception e)
{
MessageBox.Show(string.Format
("Error registering the assembly :
'{0}'.\n{1}"
, assemblyName, e.Message),
"GAC Installation Error");
}


The “UseShellExecute” property indicates whether to use the operating system shell to start the process. The default is “true”. Set this property to “false” – The process is created directly from the executable file.