Archive

Posts Tagged ‘sharepoint workflow’

Feature Installation & Activation

October 8th, 2008

Problem

We have discussed automated deployment in another post, now let’s discuss how we can install a feature. Doing an automated feature installation contains some problems if we use SDK. For these problems, please keep on reading.

Solution

The following code snippet should install a feature on the form.

SPWebApplication app = SPWebApplication.Lookup(new Uri(DefaultZone));
SPFeatureDefinitionCollection col = farm.FeatureDefinitions;
SPFeatureDefinition def = col.Add(featurefile, id, true);
app.Features.Add(id, true);

The above code installs the feature but it do not activate the feature even if we try to activate it. The activation step needs to be performed manually.

If we wanted to somehow perform this step, we can do it using the following code (this does not use the SDK):

ProcessStartInfo pStartInfo = new ProcessStartInfo();
//specify gacutil.exe whith which to start the process
pStartInfo.FileName = @”.\stsadm.exe”;
pStartInfo.Arguments = string.Format(“-o installfeature -filename {0} -force”, featureName);
pStartInfo.UseShellExecute = false;
pStartInfo.CreateNoWindow = true;
pStartInfo.WindowStyle = ProcessWindowStyle.Hidden;


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


//wait till the process completes
process.WaitForExit();

The above code will install the feature on the farm where this code is being executed. If we wanted to activate it as well, we can write down the same statements with activatefeature switch.

SharePoint , , , , , , ,

Automated Deployment for Complete Website

September 29th, 2008

As I’ve discussed different deployment options available in MOSS 2007. In this post, I will discuss how we may write down script for deploying complete website. First of all, create a project in Visual Studio 2005 and add references for Microsoft.Office.Sharepoint & Microsoft.Office.Sharepoint.Services. We will step by step look at scripts for achieving different targets. We will need to write scripts for the following steps:

- Create Web Application (along with site collection)
- Restore our website to newly created site
- Add features as required

Create Web Application

In order to create a web application, you need to write down the following code. The following code will create a new web application with a site collection within it.

// web site port
int port = Port_No;


// web site root directory
string rootDir = PATH; // it should be the path to the web app within VirtualDirectories folder


// web application name
string webAppName = Name_Of_The_Application; // this could be any string


// app pool username
string appPoolUser = User_Name; // name of the user who has rights over central administration

// build the password as a secure string
SecureString appPoolPwd = new SecureString();
foreach (char i in txtPassword.Text)
{
appPoolPwd.AppendChar(i);
}


appPoolPwd.MakeReadOnly();


Uri defaultZone = new Uri(Default_Zone);
Uri intranetZone = new Uri(Intranet_Zone);

// the database server that will host the content database(s) for this web application
string dbServer = DB_Server;

// the name of the first content database for this web application
string contentDb = Content_DB_Name; // name of the content db that will be created

// for the top-level site
string url = “/”;

// the site title
string siteTitle = Site_Title;

// the site description
string siteDesc = Site_Desc;

// this is the MS language Locale ID. see http://www.microsoft.com/globaldev/reference/lcid-all.mspx
uint LCID = 1033;

// this is the site template for the Publishing Site – Collaboration Portal merged site definition
string template = “SPSPORTAL”; // you may change it to any other template you want

// first site owner login name
string ownerLogin = Site_Owner; // this should be a user name from active directory

// site user name
string ownerName = User_Name; // this could be any descriptive name you want to give

// site owner email
string ownerEmail = Email_Address;

// get an instance of the farm from the administrative service
SPFarm farm = SPWebService.AdministrationService.Farm;

// use the SPWebApplicationBuilder to create web application and pass the local farm instance to the builder class
SPWebApplicationBuilder webAppBld = new SPWebApplicationBuilder(farm);


// assign a GUID to the IIS web application.
try
{
webAppBld.Id = System.Guid.NewGuid();
}
catch (Exception ex)
{

}

// the web application’s port
webAppBld.Port = Port;

// create a root directory for the IIS application using the SYSTEM.IO namespace
DirectoryInfo rootDirInfo = new DirectoryInfo(rootDir);

// create the root directory and assign it to the web app.
webAppBld.RootDirectory = rootDirInfo;

// set the ID of the web application pool this is not a GUID as the WSS SDK suggests
webAppBld.ApplicationPoolId = webAppName + ” Pool – ” + port.ToString();

// uses the IdentityType enumeration. This can be one of four values but for SharePoint web farms is usually a specific user account in the domain.
webAppBld.IdentityType = IdentityType.SpecificUser;

// be sure that this user exists and that the password matches the value stored in the SecureString
// appPoolPwd value.
webAppBld.ApplicationPoolUsername = appPoolUser;

// assign the secure string value as the app pool password
webAppBld.ApplicationPoolPassword = appPoolPwd;

// the default is false so that Kerberos auth. is supported. If  you’re not using Kerberos, set this to true
webAppBld.UseNTLMExclusively = true;

// default is false
webAppBld.AllowAnonymousAccess = false;

// the default is false so that SSL is not used in IIS. set this to true if the IIS web site hosting this web application should use SSL
webAppBld.UseSecureSocketsLayer = false;

// Set the default zone that appears in Alternate Access Mappings
// will probably want to change this to the URL value that most users select for browsing this web application
webAppBld.DefaultZoneUri = defaultZone;

// this will create a new content data (default is true)
webAppBld.CreateNewDatabase = true;

// set the database server that will host the database
webAppBld.DatabaseServer = dbServer;

// create a new content database for the web application
// be sure this is unique. Consider appending a GUID onto
// the end of the database name as Microsoft does when you
// use Central Admin. to create a web application
webAppBld.DatabaseName = contentDb;

// set the database username to null or an empty string
// to use Windows integrated authentication. Set both
// the username and the DatabasePassword password property
// if you want to use SQL auth.
webAppBld.DatabaseUsername = String.Empty;


//// get the server hosting the search service
SPSearchServiceInstance ssi = null;


foreach (SPServiceInstance sp in SPServer.Local.ServiceInstances)
{
if (sp.GetType().IsAssignableFrom(typeof(SPSearchServiceInstance)))
{
ssi = (SPSearchServiceInstance)sp;
break;
}


}


try
{
webAppBld.SearchServiceInstance = ssi;
}
catch (InvalidCastException ex)
{
}

// assign a comment to the web application to create
// if not set, this will default to SharePoint – <port>
webAppBld.ServerComment = webAppName + ” – ” + port.ToString();

// mention that this can take a long time
Console.WriteLine(“Web app. creation has started.”);
Console.WriteLine(“This operation an take as long as 15 minutes in our tests.”);

// create the new web application
SPWebApplication webApp = null;
try
{
webApp = webAppBld.Create();
}
catch (Exception ex)
{
}
Console.WriteLine(“Web Application created.”);

// specify time zone. You can either enumerate the GlobalTimeZones
// collection to find the right timezone or look it up on the
// Central Administration – Application Management – Web Application
// General Settings page.
webApp.DefaultTimeZone = SPRegionalSettings.GlobalTimeZones[14].ID;

// size in megabytes
webApp.MaximumFileSize = 75;

// enable alerts
webApp.AlertsEnabled = true;

webApp.Name = webAppName + ” – ” + port.ToString();

Console.WriteLine(“Updating and provisioning the web application.”);

try
{

// update the web app with the settings specified
webApp.Update();

// provisions the web application in IIS and creates the content
// database if one was specified
webApp.Provision();
}
catch (Exception ex)
{
}


Console.WriteLine(“Updating and provisioning the web application complete.”);

// perform an IIS reset operation iisreset /noforce if you’re in a web farm
Console.WriteLine(“Creating the top-level site collection.”);
Console.WriteLine(“This can also be a long running operation.”);


try
{

// create a site collection with an out of the box site definition
SPSite SiteCollection = webApp.Sites.Add(url, siteTitle, siteDesc, LCID, template, ownerLogin, ownerName, ownerEmail);

// close the site collection
SiteCollection.Close();
}
catch (Exception ex)
{
}


Console.WriteLine(“Top level site collection creation complete.”);

Restore Website

In order to restore website, write the following lines of code:

SPWebApplication app = SPWebApplication.Lookup(new Uri(Default_Zone));
SPSiteCollection clt = app.Sites;
clt.Restore(Default_Zone, @”.\YourBackup.bak”, true);

The above three (3) statements would restore your website to the newly created web appliation.

SharePoint , , , , ,

Deployment Basics

September 17th, 2008

Deployment is a very vast topic when it comes to MOSS. If you are good to go with .Net application deployments, you should know that it really sucks on sharepoint as it is not an easy trade.  Consider the following scenarios:

  • You have developed a web application using sharepoint. You have a requirement to deploy your web application with all the site contents as well as data.
  • You have already deployed the web application and you would like to provide your client with a patch to be executed on their MOSS website to get the latest site contents. At the same time, the data should not be disturbed on the production environment.
  • You have come up with a requirement to install lists, web parts, custom pages and features on the production server. You want it done through automation. I will explain it in a separate post.

Lets look at these scenarios one by one:

Deploying Complete Website

In order to deploy complete website, we use backup/restore method. It restores all site contents and any data to the destination server. Though there are certain limitations:

  • It do not restore any assemblies that you have placed in your \bin folder under VirtualDirectories folder.
  • It did not include features.
  • There could be even more limitations as you face them.

I prefer the backup/restore of stsadm utility that can be achieved by simply writing down following commands:

stsadm -o backup -url http://server_name:port -filename \\anylocation\backup.bak

We may restore this backup.bak file to any server farm using the following command:

stsadm -o restore -url http://server_name:port -filename \\anylocation\backup.bak

The above method to take backup of the site produces one bak file as you can see in the statement backup.bak. There is another way you may use Central Administration backup method, go to Operations tab and follow “Perform a backup” link. This backup produces a number of files. The same backup may also be taken using the command line by following the following commands:

stsadm -o backup -directory \\backup -backupmethod full -item portNo

The above command takes a full backup of the given port number and produces same set of files as being produced by central administration. \\backup means a backup shared folder.

In order to restore from the backup produced by the above command, follow this syntax:

stsadm -o restore -directory \\backup -restoremethod overwrite -suppressprompt -item portNo

Deploying Contents

If you would like to deploy only site contents to the production server, the best possible way is using import/export feature.  Again using stsadm utility, the following commands do the trick:

stsadm -o export -url http://servername:port -filename \\location\exported_data.cmp

The same command is being used to import site contents to production server:

stsadm -o import -url http://servername:port -filename \\location\exported_data.cmp

However, note down that features must be de-activated before you execute the import command on the destination server. Otherwise, you will experience different errors while importing.

SharePoint , , , , ,


Copyright © 2006-2011 W@rfi