Thursday, July 9, 2009

Accessing Content Type via object model in WSS

Hi All,

Today we are going to talk about the how we can use content type in object model. We will see classes and how to bind any content types with the list or document library.

We all know about the content type is that it can be treated as reusable component that can be used in list and library or any other way. Content type will always inherit from parent content type.

We can have item based content type attached to the list and document based content type attached with the document library. We cannot attach the vice versa.

First to get all the content type in our site level we will call AvailableContentTypes method which will give us SPContentTypeCollection object. Each individual object in SPContentTypeCollection is SPContentType. One more method we will use which takes the content type ID that we know and will directly give us that SPContentType.

Before that please make a note of a difference between the AvailableContentTypes and ContentTypes property of SPWeb class.

ContentTypes will return all content types only with reference to the current web but AvailableContentTypes will return you all content types of current web as well as all parent webs as well.

Ok, coming back to the point, here is a way we can proceed working with content type through the object model.

SPContentType objContentType = objWeb.AvailableContentTypes[“contenetypeID”];
objList.ContentTypes.Add(objContentType);
objList.Update();


Remember if your conenet type is about fields, then when the content type is added to the list, fields gets added to the field collection but won’t show those fields. So you need to take out the SPView class object and add the ViewFields for those new created fields through the ContentType.

Same, if we want to remove any content type, then follow the below approach.

Foreach(SPContentType objContentType in objList.ContentTypes)
{
If(objContentType.Name.toUpper() == “contenetypeID”)
{
objContentType.Delete();
break;
}
}

Monday, July 6, 2009

Retrieving data from BDC Column

Hi All,

Today we are going to discuss how to retrieve data from BDC Column. We all know that we can create BDC column once we have business application definition file installed in central administration.

If you are not clear about the BDC and about the BDC Columns, I would recommend going to this link and reading the whole series of BDC before you start reading this.

When we use this BDC Column then all fields that we have defined in FileDescriptor tag becomes available as a set in one single BDC Column. So sometimes I think, how to retrieve individual item from the single BDC Column. So here is the answer for this.

Assume that we are working with one list which has one BDC column as Order Column and it contains order information. Example ordername, order date, order qty.

Now then when you insert the list item, it will ask you for search the order based on BDC Order column which you do on Order ID. When you search and insert the record, it will fetch all the columns that you have defined in the BDC FileDescriptor tag.

To see all this in action, you create the BDC definition XML file, import and then use Business Data List web part and then play with it sometime to get to know the search field and retrieved columns.

So we have Order Date, Order Name and Order Qty as retrieved columns, then when you query the ListItems with the help of SPQuery. Let us say that we have queried on ItemId 6 by using GetItemByID method. So we will have SPListItem as objItem.

So how can we get the individual items (Order Date, OrderName and Order Qty) from one single BDC Column (i.e Order)? Here is a way to go for it.

objItem[{name of the BDC Column : {name of the propoerty}}

i.e objItem[“Order: OrderName”] //accessing with the friendly name
objItem[“Order _x003a__x0020_OrderName”] //accessing with the internal name


That’s it. Your job is done.

Sunday, July 5, 2009

Features and Solutions with WSS Object model

Hi All,

Today I am going to discuss about the features and solutions. I know you will ask me what is new in this. Well, this time we are going to talk about features and solutions in terms of object model.

We are going to discuss following things.

(1) Feature install and uninstall
(2) Feature activation and deactivation
(3) Solution install and uninstall
(4) Feature properties

We will discuss all in terms of object model. So let us start one by one. First we will take feature install and uninstall.

Now when we create feature and place it in to 12 hive folder and when we run the -o installfeature command, we actually installs the feature at farm level and when we run –o uninstallfeature command, we actually uninstalls the feature at farm level. Now how this can be done programmatically. So here is a way.

But before starting on this, we also need to learn one more object which is SPFarm class. To initiate SPFarm class, we need to know the configuration database connection path, just like ordinary connection path to connect to the database we use in ADO.NET.

For example, this can be a connection string for my configuration database.

string strConnectionString = @”server=localhost\myServer;initial catalog=SharePoint_Config_75980120-a9bf-4p71-3401-qn26385ca059;IntegratedSecurity=SSPI;”;

SPFarm objFarm = SPFarm.Open(strConnectionString);
SPFeatureDefinitionCollection objinstalledFeatures = objFarm.FeatureDefinitions;
objinstalledFeatures.Add(“{new feature}”, new Guid(“guid of feature “));


This will install the new feature at farm level. Now let’s see how to uninstall the feature from farm level.

objinstalledFeatures.Remove(new Guid(“guid of feature “));

Ok, this was about installing the feature at farm level; now let’s talk about activating feature at site level.

Considering objWeb refers to a site, then

objWeb.Features.Add(new Guid({guid of feature}));


And same to deactivate the feature from site level,

objWeb.Features.Remove(new Guid({guid of feature}));


Now let us understand what we mean by feature properties. Each feature has set of properties that you can set along with it and also built in properties that you can enumerate and check. We will insert properties in the feature.

This is how you enumerate to the properties of the feature.

foreach (SPFeatureProperty objProperty in objFeature.Properties)
{
String strPropName = objProperty.Name;
String strPropValue = objProperty.Value;
}


Now let us see how we can add and remove the properties from feature.

SPFeatureProperty objProp = new SPFeatureProperty(“{your prop name}”, “{prop value}”);
objFeature.Properties.Add(objProp);


SPFeatureProperty objProp = objFeature.Properties[0];
objFeature.Properties.Remove(objProp);


Interesting thing to note is that you do not need to call any update method on any of the objects to achieve this behavior.

Now let us take solution in to picture. By solution we mean collectively we can install features at farm level (on every server in the farm) and deploy features to those servers.

We know that every operation that I have mentioned in this article can be achieved through STSADM command, so this operation can also be performed by –o addsolution and –o deletesolution command. But here we are talking about object model, so I will show you the way to do it programmatically.

Consider the objFarm object just like mentioned above and then if you access the Solutions property, it will give you all solutions deployed at farm level.

It can be done simple as add and remove method.

objFarm.Solutions.Add({customapp.cab});
objFarm.Solutions.Remove(new Guid({guid of solution})); // Pass Guid of solution


You can enumerate through all solutions in Farm, like mentioned below.

foreach (SPSolution objSolution in objFarm.Solutions)
{
//your custom code
}


Each objSolution has different properties like Deployed, DeployedServers, Id, Name etc.

Again each DeplyoedServer represents SPServer class and has properties like Name. So in a nutshell, you can make use of these classes and play with it.

At the end I would like to summarize with what we discussed in the article with classes.

SPFeature – Represents individual feature
SPFeatureCollection – Represents collection of features at site level
SPFeatureDefinition – Represents Feature definition.
SPFeatureDefinitionCollection – Represents all feature definitions of farm
SPFeatureProperty – Individual property of feature
SPFeaturePropertyCollection – All properties of single feature
SPFarm – represents Farm
SPSolution – Represents solution file.
SPServer – Represents each server on which solution is deployed in Farm.

Thank you.

Wednesday, July 1, 2009

SharePoint 2010 is coming

Hi All,

Here I am with something very interesting and new things coming up in the market soon. Yes we are going to talk about next version of SharePoint server and also other technologies that will help out new version of SharePoint. New version of SharePoint is called SharePoint server 2010 instead of moss 2010. They have removed the name Office as now they want office to be used for office suit only. So now onwards we will call it as SharePoint Server 2010.

• If you know about Microsoft Groove then this news is for you. Groove will be known as SharePoint Workspace Manager. SharePoint Workspace Manager and OneNote will be a part of the Office 2010 ProPlus SKU.

• SharePoint Server 2010 will support only 64 - bit. It will require 64 bit Windows Server 2008 or 64 bit Windows Server 2008 R2. In addition to this, it will require 64 bit version of SQL Server 2008 or 64-bit version of SQL Server 2005. No more 32 bits.

• Forget IE 6. SharePoint Server will not support any IE browser < 7. It will require browser to be IE 7or higher. Now they are targeting only XHTML 1.0 compliant like IE 7, IE 8, and Firefox 3.x. In addition to this they are also planning to increase the level of compatibility with Firefox 3.x and Safari 3.x on non windows platforms.

• Now turn of Web Enabled Ribbon control. We all know about the ribbon controls in Office 2007. Now they will also be there for web version.

• New concept of Faceted Search is coming. Yet to hear more on this as of now. It might be included as OOTB in SharePoint 2010.

• FAST Search is coming. A new version of FAST Search for SharePoint. They will stop scaling search for documents more than 50 millions. For this purpose they will now use FAST Search.

• Support on mapping list to its own database tables offering better performance and scalability on large SharePoint list.

• Visual Studio 2010 will include support of developing web parts, feature, solutions and content types.

• We will be able to build, deploy and debug the applications right from Visual Studio 2010. Just Hit F5 and deploy and start debugging. Now this is a big relief.

• New Server explorer in Visual Studio that will allow us to navigate to lists, document libraries from site.

So be prepared to learn new version of SharePoint soon but I guess lot to explore more but yes at lot more expense. Tell you what, lot more money requires now to get all this. :) (Obviously Windows Server 2008 64 bit, SQL Server 2008 64 bit and Visual Studio 2010 – along with this, hardware supports. You can count the money now :))

I would like to thank Lar's blog , MSDN SharePoint Team Blog and Mary-Jo Foley for sharing this information.

Till now everybody has idea about the requirements and similar stuff. What actually will be the additions as development and other OOTB features is still a question for us. We all are waiting to get this information. As soon as we will find anything, I will be definitely keen to post it.

Monday, June 29, 2009

Changing redirection in SharePoint with HttpModule and HttpHandler

Hi all,

SharePoint is maintaining some redirection by it’s on like error page, by clicking on user or group it will redirect to profile page and so many other…

In one of our requirement we have to implement custom profile page and custom error page.

So for this we need to over ride default SharePoint navigation.

And that can be implemented using HttpModule and HttpHandler.

To know the concept of HttpModule and HttpHander here is great article from Gustavo Velez

So here we go for changing Error page (check comments between the code for understanding)

class RedirectErrorModule : IHttpModule
{

//Custom error page relative URL
const string errorUrl = "/_layouts/CustomForder/CustomError.aspx";

// implement dispose method to dispose used object
public void Dispose()
{
//dispose you objects
}

public void Init(HttpApplication context)
{
//Define event for application error so that you can override.
context.Error += new EventHandler(context_Error);
}

//implement error event
void context_Error(object sender, EventArgs e)
{
string strURL = string.Empty;

//Current site URL
string strSiteURL = SPContext.Current.Site.Url;

//Current web url
string strWebURL = SPContext.Current.Web.Url;

//Clearing context error
HttpContext.Current.Server.ClearError();

//Clearing the response
HttpContext.Current.Response.Clear();

//redirecting to our custom error page.
HttpContext.Current.Response.Redirect(strWebURL + errorUrl, false);
}

}


After doing this we need to add entry in web.config for the same.

Enter following tag in <httpModules> section of your web.config
<add name="CustomHttpModule" type="<<Full assembly name with class>>" />

Check syntax from existing tags from web.config.

The same way we can implement any redirection with the use of end request Event.
public void Init(HttpApplication context)
{
//end request handler
context.EndRequest += new EventHandler(context_EndRequest);
}

//End request implementation
void context_EndRequest(object sender, EventArgs e)
{
HttpApplication httpApp = sender as HttpApplication;
HttpContext context = httpApp.Context;
string httpUrl = context.Request.Url.ToString();
string strWebURL = string.Empty;
string struserID = string.Empty;
//compare your URL and redirect it to custom one
// this is example to redirecting userdisp page (profile page) to custom page.
if (httpUrl.ToLower().Contains("/_layouts/userdisp.aspx"))
{

//implement business logic and generate url if needed
HttpContext.Current.Server.ClearError();
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.Redirect(<<Custom URL>>);

}
}


And same can be incorporated in one class no need to create another one.

And remember in web.config enter your module at the end after all SharePoint’s event handler so it will not create any problem. There is not at all problem if you write it before SharePoint’s but better to play safe :)



Share your SharePoint Experiences with us...
As good as the SharePointKings is, we want to make it even better. One of our most valuable sources of input for our Blog Posts comes from ever enthusiastic Visitors/Readers. We welcome every Visitor/Reader to contribute their experiences with SharePoint. It may be in the form of a code stub, snippet, any tips and trick or any crazy thing you have tried with SharePoint.
Send your Articles to sharepointkings@gmail.com with your Profile Summary. We will Post them. The idea is to act as a bridge between you Readers!!!

If anyone would like to have their advertisement posted on this blog, please send us the requirement details to sharepointkings@gmail.com