Tuesday, February 1, 2011

Planning Content Type Hubs in SP2010

when you are developing enterprise application for large organization some fields are common across all brands such as brand name, brand logo, SEO title, SEO description etc..

How will you handle this common fields across farms?

Scenario (P&G only for reference)

Assume that we are designing architecture for P&G products, normally P&G has lot of products (Pantene, Olay, pampers etc...)but, all these products have some common fields such as Product logo, product name, SEO title, SEO description etc... How will you handle this?

SharePoint 2007

In SharePoint 2007 usually what we do? we go and create separate content type for this called brand content type and deploy this into site collection but, here the problem is we need to go to each and every web application and install site content type feature , there is no central location to place common fields, in future if you want to update particular column, you have to go to each and every web application and update

SharePoint 2010

In SharePoint 2010 there is a new concept called Content Type Hubs, through this you can manage all common fields centrally and publish it to the other web applications, so in 2010 the other web applications can subscribe these content types and pull down the published content types from the CTH and also can get updates when ever changes happened in published content types

Here, mainly you need to concentrate on three things

Site collection to host the content types and act as a Content Type Hubs

Managed metadata service application( to expose content type hub to each and every web application)

Web applications ( to subscribe content type hubs)

Advantages

No need of going to each and every web application and installing the content types and if you want to do updates on particular content type you can simply do it on Content Type Hubs and push it down, so managing the content types easy

In reality the content types changes frequently like adding new fields or creating new content type etc... It's easier to coordinate this from a single location and make sure that xml files are correctly set up across all web applications/farms.

Tuesday, January 18, 2011

Moving data from one site collection to other and make it up

In SharePoint, we need to concentrate on lot of things while moving site from one environment to another environment and make it up

Based on my experience, I am sharing some of the details with you guys

If you are using VSIDE to develop your site for example creating custom web parts, master pages, css files, features etc...and then developed using VSIDE Then WSP package is best solution but this will solve only half portion of your requirement that means you are deploying static content means the data which will not change frequently

But, you need to think about how the dynamic data will be deployed from one environment to another environment dynamic content means the content which is authored by client like new article page, new documents uploaded into document library etc...

here you need to use either content deployment or Export /Import but in my SharePoint journey I have used both, choosing the right option is depends on how our environment is setup?

If your environment is farm environment and having multiple nodes with reliability and security then we can use content deployment option because content deployment will transfer the data on wire so, make sure that there should not be any connection failure while doing content deployment

Before doing content deployment you need to consider lot of things

1. The source farm and destination farm should be identical, assume that you have installed latest service pack or language packs on source farm and the same is not available on destination farm then this will give problem and the content deployment will fail.

2. The destination site collection should be blank site collection if not then the job will fail because when The first time the content deployment job runs, the correct template and all associated configuration settings will be applied to the destination server if site collection already contains data then the job will fail.

3. The disk space in source and destination farm should be enough otherwise disk allocation problem will occur because when job starts it will create a cab file in source temporary folder and export into destination farm temporary folder.

4. if you have any custom features to install in destination server just install before running your job but don't activate the features because if you activate that will create data in content database so, this may lead to content deployment fail.

5. Don't run the content job's parallel this may lead to content job fail.

Normally SharePoint enterprise application will have two farm topology authoring and production farm. Authoring farm works on AD and production is internet facing site and works on FBA, the content authors will upload the data into authoring site and the same will be available in production site through content deployment. There is no specific requirement to have only two farm topology, based on requirements you can build multi-farm,I worked for one client who is having five farm topology

I suggest to use content deployment because you can do differential content deployment that means whatever changes made recently will be only moved to one environment to another environment so, this will not override existing content

Wednesday, June 16, 2010

Creating site columns thorugh SQL query

Sometimes, you may feel pain while creating site columns and content types through CAML, because you need to know CAML syntax for each and every site column data type, but you can avoid this by simply running one sql query

Normally in SharePoint when we create any site column or content type through SharePoint user interface that will go and stored into content database in the form of CAML, so we can run simple sql query and get that data and build it into features and deploy the same in some other environment means production

Steps

1. create sitecoumns and content types using SP user interface

2. connect to sql server database

3. Execute below query

"Select definition from content types where definition is not null"

4. you can see all site columns and content types in form CAML

5. just extract that and build a features and deploy

Friday, June 11, 2010

Access denied error when code runs with SPsecurity.RunWithElevatedPriviliges

Today, while reviewing code ,I have found some thing, which will give access denied error if our code runs with SPSecurity.RunWithElevatedPriviliges and AllowUnsafeUpdates = true

Check the piece of code which I have written below that gives access denied error

using (SPSite site = new SPSite(SPContext.Current.Web))
{
SPList list = web.Lists[listUrl];
SPFieldCollection fieldCol = list.Fields;
SPListItem newItem = list.Items.Add();
foreach (SPField field in fieldCol)
{
if (!field.Hidden && !field.ReadOnlyField)
{
if (field.Title.Equals(question, StringComparison.OrdinalIgnoreCase))
{
newItem[field.Title] = answer;
web.AllowUnsafeUpdates = true;
newItem.Update();
web.AllowUnsafeUpdates = false;
break;
}
break;
}
}
}

The main problem here is that the current request will execute under the priviligies of anonymous user credentials because user is a anonymous user, so this code always gives access denied error

The web object is created using SPContext, so this web object is runs under credentials of spcontext, so this always gives problem

using (SPSite site = new SPSite(SPContext.Current.Site.ID))
{
using(SPWeb web = site.OpenWeb())
{
SPList list = web.Lists[listUrl];
SPFieldCollection fieldCol = list.Fields;
SPListItem newItem = list.Items.Add();
foreach (SPField field in fieldCol)
{
if (!field.Hidden && !field.ReadOnlyField)
{
if (field.Title.Equals(question, StringComparison.OrdinalIgnoreCase))
{
newItem[field.Title] = answer;
web.AllowUnsafeUpdates = true;
newItem.Update();
web.AllowUnsafeUpdates = false;
break;
}
break;
}
}
}
}

The above code will run under credentials of system account that means with full permissons so this will execute fine here the difference is we are creting spsite object with url not from current context and the SPsecurity.RunWithElevatedPriviges will work fine here

Sunday, February 28, 2010

Another reusable component - Site Columns and Content Type generator

I have seen so many people struggling to create sitecolumns and contenttypes in CAML format while developing sharepoint aplications. Some people spend days to complete these and some people spend hours depends on their knowledge on sharepoint, recently I have created webpart which will give sitecolumns and contenttypes in the form of CAML the main advantage of this is you will get complete features folder

you just need to deploy this into ur 12 hive features folder just install and activate

I just completed beta version of this webpart need to test and upload this into my blog

I hope this will save lot of time for each and every developer, min 1hour

First create your site columns using sharepoint GUI

Creating Site Columns

Site Actions

Modify All settings

Galleries

Site Columns

Create

Enter Column name , choose appropriate data type and click on create

This will create site column for you in sharepoint create as many you want

Creating Site Content Types

Site Actions

Modify All settings

Galleries

Site Content Types

Create

Provide Name and click on ok, this will create your content type

Adding columns to the content type

Go to content type gallery

Select content type the one which you created recently

Click on add from existing site columns

Select site columns and click on Ok

This will create content types for you

Now install RSM.Exporter webpart on respective webapplication

Add that webpart in any page of ur web application the webpart will dispaly as shown in fig



you just enter your site column feature name on site columns textbox and provide description for your feature and do same thing for site content types also and click on create sitecolumns and content types feature button

Now go to your "C://" drive and check "RsmmTechno" folder there you can find two folders one is for site columns and another one is for content types

So we have successfully completed creation of site columns and content types features now we need to create a page layouts based on content types

Steps to create page layouts :

Select content type which you want to attach to the page layout from content type drop downlist

Enter page layout feature name

Enter page layout name

Select fields which you want to render while creating page using page layout from the list box

Click on create page layout feature

Once again go to your "C://" drive and check "RsmmTechno" folder there you can find page layout feature along with site columns and content type as shown in below fig



Just open each and every folder you can find feature.xml file and element.xml files as shown in below fig







Now, Install and activate features



Create a page using page layout




I think this will help you guys a lot, no need of breaking heads what is the syntax for this sitecolumn content type and page layout etc...

Wednesday, February 24, 2010

Deploying Custom Resource files into "App_GloablResources"

Normally, when you create new webapplication in sharepoint the system will automatically copies all resources files from \\12\Config\Resources folder to respective webapplication Virtual directory "App_GlobalResources" folder this will work fine in normal scenarios, so in deployment you can just place your custom resources files into 12 hive Resources folder and create webapplication everything will work fine

But some times we may have to install some kind of upgradion packages with out affecting existing system which contains custom resource files? What would be the approach if we want to place all custom resources files into already created webapplication???

Options to deploy resources files in existing webapplication

1. We have to create deployment document and saying that copy and paste all the files into "App_GlobalResurces" folder

2. Create a separate feature and while activating that feature write a code to copy and paste all the files from feature folder to "App_GlobalResources" folder

Personally I prefer second approach

Please check below image to find out folder structre and where all resource files are placed



Just create feature with event receiver class in the FeatureActivated event write below code

using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;
using System.IO;
using Microsoft.SharePoint.Utilities;
namespace RSMTechno.DeployGlobalResources
{
class DeployGlobalResources : SPFeatureReceiver
{

public override void FeatureActivated(SPFeatureReceiverProperties properties)
{

string sourcePath = string.Empty;
SPSite site = (SPSite)properties.Feature.Parent;
SPWeb web = site.OpenWeb();
//sourcepath is the path where all resource files are stored
sourcePath = string.Format("{0}\\FEATURES\\{1}\\", SPUtility.GetGenericSetupPath("TEMPLATE"), properties.Definition.DisplayName);
try
{
SPWebApplication webApp = web.Site.WebApplication;

foreach (SPUrlZone urlZone in webApp.IisSettings.Keys)
{
SPIisSettings settings = webApp.IisSettings[urlZone];
string destinationPath = Path.Combine(settings.Path.ToString(), "App_GlobalResources");
string[] filePaths = Directory.GetFiles(sourcePath, "*.resx");
foreach (string filePath in filePaths)
{
string fileName = Path.GetFileName(filePath);
File.Copy(filePath, Path.Combine(destinationPath, fileName), true);
}

}
}
catch (Exception)
{
throw new Exception("Failed to copy files");
}
}
}
}

Then build the application and deploy and activate the feature everything will work fine

Click here to download source code RSMTechno.DeployGlobalResourceFilesCode

Friday, January 8, 2010

Why people are not using sharepoint designer?

I have seen some people,they are not interested to use sharepoint desinger I dont know why? even though they are not familiar with syntaxs of sharepoint webcontrols , field contrls while desingning page layouts, customizing master pages and sharepoint controls

I dont know about other sharepoint developors, but whenever I get chance I love to use sharepoint desinger to customize my pages

Because if we use sharepoint desinger our development will be faster, no need of sitting on Visual Studio and breaking head for syntaxs(not only for syntaxs it provides lot of other features like workflows,reporting, data view webparts etc...)

Recently I asked one sharepoint senior developer why you are not using sharepoint desinger for designing the pagelayouts?

I got very silly answer that he feels that when we edit page using sharepoint designer that will go and sit in to content database

Yes his answer is correct but in which situation, when we save the page and checked- in

But in production we are going to deploy all these things as a separate features so there will not be any problem regarding perfomrance point of view

Think how much time it will take to develop same in VSIDE, personally I am 100% sure that it will take more time to design a page using VSIDE, when we compare with sharepoint desinger if you are not familiar with sharepoint designer

So guys please use sharepoint designer to make your share point development faster

Soon you will except some good stuff on sharepoint designer

Saturday, December 19, 2009

SharePoint Solution Architecture

Solution Architecture

I am just going to share my thoughts on creating solution architecture for sharepoint application

Here I have created so many projects don't scare, you can change this according to your requirements

Normally I split solution based on requirements, but I would like to discuss general approach for SharePoint projects based on my knowledge I hope this will help you at least some part of your application




RSMTechno.CodeBehind
RSMTechno.Common
RSMTechno.Controls
RSMTechno.DataAccessLayer
RSMTecno.SolutionDemo
RSMTecno.BusinessLayer

RSMTechno.CodeBehind

I have created some page layouts and master page and deployed in to SharePoint environment , later I felt that I wanted to add some code to the master page to customize navigation bar…or some code to the sitepages,here I strucked because by default SharePoint will not generate code behind files to master page and site pages

So what I have to do?

Either I have to write inline code or I have to create separate class library and attach it to the master page or site pages

Suppose if I write inline code to the page and run the application everything works fine but if I customize the page using SharePoint designer and run the application there the problems comes because in SharePoint the customized page run through the safe mode parser, which will block any inline code from running

So what is the solution for this? what we have to follow to resolve this?

We need to follow second approach .

We have to create separate class library and register that in page using register tag , our RSMTechno.CodeBehind libarys is created for that it contains all the class files inherited from Microsoft.SharePoint.Publishing.PublishingLayoutPage or System.Web.UI.MasterPage or Sytem.web.UI.WebPartPage

Check the sample code provide below

Layout Page

using System;
using System.Web.UI;
using Microsoft.SharePoint.Publishing;

namespace RSMTechno.Pages {
public class RSMLayoutTemplate : PublishingLayoutPage {
}
}

WebPartPage


using System;
using System.Web.UI;
using Microsoft.SharePoint.Publishing;

namespace RSMTechno.Pages {
public class RSMWebPartPage: WebPartPage
{
}
}


RSMTechno.Common

This application is created mainly for utility methods and common functionally across all the functionality's like utility classes(ProfileUtility etc...)

RSMTechno.Controls

This library project is created mainly for creating custom controls
While creating webparts I prefer that first create custom control and create object for that in your webpart class ,the main advantage of this is in future suppose if your client want to shift to .net based application because of some reasons, he can easily move in to .net application because you have developed all the components using webcontrol class so you can reuse same controls in your .net application

Sometimes you may need to override dropdownlist or datagrid or any .net control to provide reusability you can use this project to handle such kind of exteded controls

I have provided sample code here

Custom Controls

Normal Custom Cotntrol

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace RSMTechno.Controls
{
[ToolboxData("<{0}:DemoControl =server>")]
public class DemoControl : WebControl
{
//create your control
protected override void OnInit(EventArgs e)
{
//add to controls collection
base.OnInit(e);
}
}

Extended controls

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace RSMTechno.Controls
{
[ToolboxData("<{0}:DropListwDefault runat=server>")]
public class ExtendedDropDownListwDefault : DropDownList
{

public ExtendedDropDownListwDefault()
{
DataTextField = "name";
DataValueField = "id";
this.AppendDataBoundItems = true;
AddDefault();
}

public void AddDefault()
{
ListItem defaultItem = new ListItem();
defaultItem.Selected = true;
defaultItem.Text = "--";
defaultItem.Value = Guid.Empty.ToString();
this.Items.Add(defaultItem);
}

}
}


WebParts class

namespace RSMTechno.WebParts
{
public class DemoControl : system.web.UI.WebControls.WebParts.WebPart
{

}
DemoControl _dc = null;
protected override void OnInit(EventArgs e)
{
_dc = new DemoControl();
this.Title = "Demo Control….";

this.Controls.Add(_dc);

base.OnInit(e);
}
}

RSMTechno.DataAccessLayer

I want to get data from other database servers and display it on web part, here we need to write a code to interact with different database servers this project library is created for that purpose only

You can find one dbml file which will interact with northwind database through LINQtoSQL

But, rarely we use this library project because MOSS has provided some OOTB functionality to interact with external database servers

RSMTecno.SolutionDemo

This is the heart of the solution; this will take care of all the things like installing the web parts, site definitions, master pages and user controls etc... using features

Here, you can find some folders which are mapped to exactly 12 hive folder structure in sharepoint environment like CONTROLTEMPLATES,RESOURCES,LAYOUTS and FEATURES etc...

While deploying what are all things you have placed uder these folder structure will be automatically dumped in to sharepoint 12 hive environment

I am using wspbuilder for building and deploying features

I have posted one article on how to build and deploy features using wspbuilder please go through that if you have any doubts

RSMTechno.BusinessLayer

This will seaparate your business logic and it provides more reusability and maintanence

Each and every component will interact with this to fetch data from sharepoint document library or list or xml file or from third party resources and populate accordingly


Some people asked me why I have to create five projects I will use one or two , yes ofcourse if you want you can use but what about maintenance and reusability just think?

Building solution is not a simple task you have to consider lot of things like maintenance, reusability,supports testdriven development etc...

Let us take this simple scenario in future, if I want to add one new control in to my sharepoint application? what are all things do I need to do? I will just go and simply create one server control under RSMTechno.Control project and build that project and deploy that assembly into GAC or BIN

See how much simple it is with out building or touching other components we are completing our task

There may be some delay passing objects from one layer to another layer but I feel that is not considered amount what ever you write that should be in reusable manner and should be easily maintenable

If you want you can add different projects in the solution like RSMTECHNO.WebServices etc... based on your requirements

But be carefull unnecessary if you pass your objects in multiple layers that will affect on performance