Thursday, December 9, 2010

Ghosted and Unghosted Pages in SharePoint

SharePoint web sites can contain two kinds of pages: Ghosted and Unghosted

Ghosted Page in SharePoint
A ghosted page is a page in SharePoint website which is not stored in the database instead it reference to a file which exists in the server’s file system. These reference files are common for all the website/site collection within that SharePoint server, i.e., if you modify a reference file then that change will reflect in all the websites/site collections within that SharePoint server automatically. So we can say these reference files are used as template.

The default master page of SharePoint “default.master” is a well known example of ghosted page. “default.master” page is located in the directory "C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\TEMPLATE\Global". If you do some changes in the “default.master” then this change will automatically reflect in all the websites within that SharePoint server.

To create a new site in SharePoint, a site template is used. Site template contains description of all the pages, webparts within the pages, master page used, custom lists, etc. for the web site to be created. You can define a page ghostable in the “onet.xml” file of the site template.

Unghosted Page in SharePoint
All the pages in a SharePoint website which are stored in the content database are referred as unghosted pages. All the unghosted pages are specific to that SharePoint website only, i.e., changes done in an unghosted pages will not reflect in other websites within that SharePoint server.

If a new website is created with a site template which contains a page defined as “unghostable” in the “onet.xml”, then that page will be stored in the content database of new website created and will not reference to the page available in the site template folder.

If a ghosted page is modified in the SharePoint designer, it will become unghosted. For example if a master page is customized in SharePoint Designer, SharePoint stores a modified version of the master page in the content database and also it breaks the reference to the “default.master” file on the “C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\TEMPLATE\Global“.

Wednesday, December 8, 2010

Dynamically changing the master page in SharePoint 2007

http://www.codeproject.com/Articles/43377/SharePoint-Dynamically-Change-Master-Page.aspx

Tuesday, December 7, 2010

Creating and Deploying Custom aspx Page as Feature and Solution Package

There are two ways to deploy a custom aspx page in SharePoint.

1. Using VseWss extensions project. Here you won't need to create a solution package manually. All the files need to create a solution package are created by the VS extensions itself. See the related Post Deploy Custom Css file in 12 hive. You can use same method to deploy your Custom aspx page.

2. The second way is creating a solution package around your asp.net webapplication so that the pages in the webapplictaion can be deployed in sharepoint. This requires you to manually create all the solution related files (I mean manifest.xml, feature.xml, elements.xml and .ddf file).

In this Post, we will create a solution package manually for a asp.net webapplication project, so that the custom aspx page created in asp.net web application can be deployed in SharePoint's Layouts folder.

Below are the Steps that you can follow :

1. Create a New WebApplication Project.

2. Create a new folder "MyCustomFolder" in the solution explorer and Add your custom aspx page (along with cs file) under it.

3. Add two more xml files in the same folder with names as elements.xml and feature.xml.

The Elements.xml File should look like below -

http://schemas.microsoft.com/sharepoint/">






Note : Add Module name as “Pages” and url as ” _Layouts “

The Feature.xml File should look like below -



4. Now, Create another xml file in the Project and name it as manifest.xml

The manifest.xml should look like below -

http://schemas.microsoft.com/sharepoint/" SolutionId="A5A9C1C2-4EBF-40db-935F-66085A9E0BE8">













Note : If you are using some code behind with your aspx page, then change the Inherit tag in the aspx page to inherit from the assembly of the project.

For e.g. change the tag to

Inherits="NameSpace.Class, NameSpace, Version=1.0.0.0, Culture=neutral, PublicKeyToken=2ef8d0c4bab8980b" Debug="true"

You dont need to deploy .cs file with the Project. The code is accessed via its .dll .

5. Finally Create the .ddf file ( its simply a text file with name as .ddf)

.ddf file would be something like below -

.OPTION Explicit ; Generate errors.Set CompressionType=MSZIP

.Set UniqueFiles=Off.Set

DiskDirectory1=Package

.Set CabinetNameTemplate=Project.CustomPage.wsp

manifest.xml

bin\ Project.CustomPage.dll

.Set DestinationDir=TEMPLATE\LAYOUTS\CustomPage.aspx

;sets the feature directory.

Set DestinationDir=CustomPageFolder

;adds the feature to the feature directory

MyCustomFolder\feature.xml

;adds the element to the feature

MyCustomFolder\elements.xml

I have created an empty folder in the Project with a name as “Package” to save the .wsp file in it.

6. Sign the project with a key (Project Properties -> "signing tab" and browse your .snk key) and Build the Project.

7. Now, Add and deploy the .wsp solution which is under Package Folder in SharePoint using stsadm commands.

Using External Javascript, CSS or Image File in a WebPart

Button Testbutton;
Image img;
string imagePath;

// Referring External Javascript
ClientScriptManager cs = Page.ClientScript;
// Include the required javascript file.
if (!cs.IsClientScriptIncludeRegistered("jsfile"))
cs.RegisterClientScriptInclude(this.GetType(), "jsfile", "/_wpresources/MyWP/1.0.0.0_9f4da00116c38ec5/jsfile.js");

Test :
Testbutton= new Button();
Testbutton.Text = "Click me";
Testbutton.OnClientClick = "jsfile_Function()"; // specify function name here
this.Controls.Add(Testbutton);

// Refering External CSS
Microsoft.SharePoint.WebControls.CssLink cssLink = new Microsoft.SharePoint.WebControls.CssLink();
cssLink.DefaultUrl = "/_wpresources/MyWP/1.0.0.0_9f4da00116c38ec5/styles.css";
this.Page.Header.Controls.Add(cssLink);

// Using External Image
imagePath = "/_wpresources/MyWP/1.0.0.0_9f4da00116c38ec5/Image.jpg";
img.ImageUrl = imagePath;
img.ID = "image1";

this.Controls.Add(mybutton);
this.Controls.Add(img);

Impersonation in Sharepoint (RunWithElevatedPrivileges)

Although not recommended, there may be times when you need your code to perform certain functions that the current user does not have the necessary permissions to perform.

The SPSecurity class provides a method (RunWithElevatedPrivileges) that allows you to run a subset of code in the context of an account with higher privileges than the current user.
The premise is that you wrap the RunWithElevatedPrivileges method around your code. And also In certain circumstances, such as when working with Web forms, you may also need to set the AllowSafeUpdates method to true to temporarily turn off security validation within your code. If you use this technique, it is imperative that you set the AllowSafeUpdates method back to false to avoid any potential security risks.

Code example

{
SPSite mySite = SPContext.Current.Site;
SPWeb myWeb = mySite.OpenWeb();

//Using RunWithElevatedPrivileges

SPSecurity.RunWithElevatedPrivileges(delegate()
{
// Get references to the site collection and site for the current context.
// The using statement makes sures these references are disposed properly.

using (SPSite siteCollection = new SPSite(mySite.ID))
{

using (SPWeb web = siteCollection.OpenWeb(myWeb.ID))
{

web.AllowUnsafeUpdates = true;

try
{
//Your code
}

web.AllowUnsafeUpdates = false;

//siteCollection = null;
//web = null;

}

Retrieving large number of Items from sharepoint list

If you have to reterive a large number of Items and also need a better performance then you should use one of the methods below :

1. Using SPQuery

2. Using PortalSiteMapProvider Class

Lets see the examples for both the methods :

Our Query - Query to get all the Items in a list where Category is "Sp2007"

SPQuery -

// Get SiteColl
SPSite curSite = new SPSite("http://myPortal");

//Get Web Application
SPWeb curWeb = curSite.OpenWeb();

// Create a SPQuery Object
SPQuery curQry = new SPQuery();

// Write the query
curQry.Query = "
SP2007
";

// Set the Row Limit
curQry.RowLimit = 100;

//Get the List
SPList curList = curWeb.Lists(new Guid("myListGUID"));

//Get the Items using Query
SPListItemCollection curItems = curList.GetItems(curQry);

// Enumerate the resulting items
foreach (SPListItem curItem in curItems)
{

string ResultItemTitle = curItem["Title"].ToString();

}

PortalSiteMapProvider class -

The class includes a method called GetCachedListItemsByQuery that retrieves data from a list based on an SPQuery object that is provided as a parameter to the method call.
The method then looks in its cache to see if the items already exist. If they do, the method returns the cached results, and if not, it queries the list, stores the results in cache and returns them from the method call.


// Get Current Web
SPWeb curWeb = SPControl.GetContextWeb(HttpContext.Current);

//Create the Query
SPQuery curQry = new SPQuery();
curQry.Query = "SP2007";

// Get Portal Map Provider
PortalSiteMapProvider ps = PortalSiteMapProvider.WebSiteMapProvider;

PortalWebSiteMapNode pNode = TryCast (ps.FindSiteMapNode (curWeb.ServerRelativeUrl), PortalWebSiteMapNode);

// Get the items
pItems = ps.GetCachedListItemsByQuery(pNode, "myListName_NotID", curQry, curWeb);

// Enumerate all resulting Items
foreach (PortalListItemSiteMapNode curItem in pItems)
{
string ResultItemTitle = curItem["Title"].ToString();
}

Monday, December 6, 2010

Common Differences (Sharepoint 2007 VS Sharepoint 2010))

SP 2007 Object Model Vs SP 2010

In Sharepoint Object model there are two Important namespaces.

In SharePoint 2007 - The Microsoft.Office.Server namespace is the root namespace of all Office Server objects and Microsoft.SharePoint is the root namespace for all WSS objects.

Hive :

In SharePoint 2007 - It has "12 hive" structure where all SharePoint resources are deployed.

In SharePoint 2010 - Microsoft has apparently added three new folders to its hive and calling it as "14 Hive"
* UserCode – files used to support sandboxed solutions
* WebClients – used for the client Object Model
* WebServices – New .svc files

Foundation :

SharePoint 2007 : Wss 3.0 was required for accessing all common SharePoint API's

SharePoint 2010 : SharePoint Foundation 2010 is required to provide base API's.

API's :

SharePoint 2007 : No API was available for Code to Interact with SharePoint site through Client side scripts (side Note : You can do it by calling Sharepoint web services using javascript )

SharePoint 2010 : MS has introduced Microsoft.SharePoint.Client namespace that enable you to interact with SharePoint sites through scripts that run in the browser from Microsoft .NET Framework managed code, and inside Microsoft Silverlight applications.

SharePoint Foundation 2010 VS SharePoint Server 2010

SharePoint Foundation 2010 : Is is for smaller organizations or departments looking for a low-cost entry-level or pilot solution for secure, Web-based collaboration.

SharePoint Server 2010 : SharePoint Server 2010 builds on the Microsoft SharePoint Foundation 2010 infrastructure to provide a true enterprise portal platform.Any features that are available in SharePoint Foundation 2010 are also available in SharePoint Server 2010.

Features :

SharePoint Foundation 2010 : Coordinate schedules, organize documents, and participate in discussions through team workspaces, blogs, wikis, and document libraries on the platform that is the underlying infrastructure for SharePoint Server.

SharePoint Server 2010 : In addition to all the Features in SharePoint Foundation 2010, the server includes some additional features for every component available in Foundation 2010. Like Along with the BCS, sharePoint 2010 server also includes External data in search, Secure Store service,External Data Web Parts,Profile pages,External data in workflow,Rich client integration and lot more...

Stsadm Vs Windows PowerShell

Def :

Stsadm Tool : Microsoft Office SharePoint Server 2007 includes the Stsadm tool for command-line administration of Office SharePoint Server 2007 servers and sites.Stsadm is located at the following path on the drive where SharePoint Products and Technologies is installed: %COMMONPROGRAMFILES%\microsoft shared\web server extensions\12\bin. You must be an administrator on the local computer to use Stsadm.

Windows PowerShell : Windows PowerShell™ command-line interface is a new command-line tool and supporting scripting language from Microsoft that complements Cmd.exe in the Windows administration context. In the SharePoint administration context, Windows PowerShell supersedes the Stsadm.exe administration tool.

Return Parameter :

Windows PowerShell :Unlike most command-line tools, which accept and return text, Windows PowerShell is built on the Microsoft .NET Framework and accepts and returns .NET Framework objects.

Access to File System :

Stsadm : It does not allow you to access file system,registry ans so on..

Windows PowerShell :Like many shells, Windows PowerShell gives you access to the file system on the computer. In addition, Windows PowerShell providers enable you to access other data stores, such as the registry and the digital signature certificate stores etc..

SharePoint 2007 Lookup Column vs SharePoint 2010

Cascade Delete

SharePoint 2007 : The Lookup column in SharePoint 2007 does not support cascade delete. i.e. if If an Item\Value in the the look-up list is deleted, then all those items referencing that value (as look-up value) will not be delete. This will rather present you with various errors and can also cause errors on the Site page if your are using one of them as filters.

SharePoint 2010 : If an Item\Value in the the look-up list is deleted, then all those items referencing that value (as look-up value) in other lists will also be deleted.

Restrict Delete

SharePoint 2007 : Does not have option to restrict deleting of lookup list items\values.

SharePoint 2010 : Choosing this option would restrict the users from deleting an item in the column in the Look-up list, if the value is being used in some other lists.

Number of columns displayed

SharePoint 2007 : This will only display the chosen lookup column in the referencing list.

SharePoint 2010 : You can now display additional columns from the look-up list, along with the chosen lookup field.

SSP Vs Service Applications

Differences between SSP and Service Application are :

What are :

SSP : A Web application that contain all the services proived by sharepoint, and can be shared by various web applications. Some of the services are Search, Infopath,User Profiles etc.

Service Application : The Services that use to be together in SSP, now run independently as a Service Application.


Build -In :

SSP : Shared Services Provider (SSP) was only a part of Office SharePoint Server 2007.

Service Applications : The service application architecture is however, built into Microsoft SharePoint Foundation 2010 itself.

SSP Administration Site :

SSP : They require a SSP administration site to configure the associations with web applications.

Service Application : They are running independently and can be individually associated with the one or more web applications.

Web application's Burden :

SSP : Any Web application associated with the SSP has to take the burden of all the shared services in that SSP.

Service Application : Each web application now have a "Service application group" where they can just add the Services that they need.

Replication :

SSP : The Services where configured in SSP itself and were not replicated. All web applications will use one set of srevices.

Service Application : If the service is needed to be shared between few web applications, the service is re-configured and added into each web application's custom service connection group.

Business Data Catalog (BDC) VS Business Connectivity Services (BCS)

What is Business Connectivity Services in SharePoint ?

SharePoint 2010 provides a new set of technologies known as Business Connectivity Services for retrieving, editing, updating, and deleting data from external systems(for e.g. data from ERP or CRM database). BCS enhances the SharePoint platform’s capabilities with out-of-box features, services and tools that streamline development of solutions with deep integration of external data and services.

How is BCS Different from BDC in SharePoint 2007 ?

Even though the BDC made it relatively easy to create read-only solutions that display data in the Business Data List Web Part, it was not so simple to create a solution that enabled users to make changes and write that data back to the external store.

BCS, on the other hand, provides you with Read-Write capable connectivity from Client and Server to Database, WCF/Web Services and .Net Sources.

A Developer can now use SharePoint Designer 2010 and VS 2010 rapid development tools to access external data. For e.g. you can now create read-write connections to external database from SharePoint designer and then can create webpart\other solutions to surface that data.

The BCS data can further be used in other SharePoint Fetaures such as Business Intelligence,Collaboration and in Enterprise Search.

Sharepoint 2010 Workflow Tutorial - I

See the Video Tutorial @ SharePoint 2010 Workflow Video Tutorial
A quick view on whats new with SharePoint 2010 Workflows :

1. SharePoint 2010 workflows are build upon the workflow engine provided .Net Framework 3.5.

2. List and Site workflows - In addition to the SharePoint lists we can now create workflows for SharePoint sites as well. These are called as "Site Workflows".

3. SharePoint Designer 2010 Changes - MS has provided a new graphical workflow designer for designing workflows. These workflows can be deployed in SharePoint Server directly from the designer.

4. Editing Out-of-Box workflows - Another Improvement in SharePoint Designer 2010 workflows is that it now allows you to edit the out-of-the-box workflows that come with SharePoint.

5. Re-usable Workflow - In addition to above, with Designer 2010 you can also create reusable declarative workflows. That means unlike SharePoint 2007 designer workflows, you don't have to bind a workflow to a specific list. You can resuse them by binding it to more than one list or multiple lists.

6. User profile in Workflow - User Profile data can now be bound to properties in workflow. This will make it possible to get information about the SharePoint user profile in the workflow itself.

7. Moving SharePoint Designer Workflows - The resuable designer workflows can now be moved to another SharePoint server or to Visual Studio 2010 with a workflow .wsp file. "Save as Template" command can be used to create the WSP file for the workflow.

8.Changes in List Events - SharePoint 2010 adds four new workflow Event Receivers for list based workflows. The four workflow event receivers available are Starting, Started, Postponed and Completed. These are similar to other SharePoint list\library event receivers and they execute code on the server in response to the event.

9. Workflow Templates - To make development easier, Visual Studio 2010 includes event receiver project types to make using the workflows and events fairly simple.

Now to Start developing workflows what do you need.

1. SharePoint designer - If you need to customize the Out-of-box workflow or wants to create a "not so complex" reusable workflow. You can however, deploy your designer workflow in any sharepoint enviornment using a wsp file. See Move Designer workflow using wsp

2. Microsoft Visio - If you want to design a workflow which has complex workflow chart, you can design it in Viso and later it can be pulled in to SharePoint designer. Also, note that you can only create Sequential workflows in viso.

3. Creating Worflow in VS - Visual Studio 2010 provides templates to create workflows and creates events for the workflow.

Lets look at some of the Videos available for creating workflows using all above methods.SharePoint 2010 Workflow Video Tutorial