Monday, April 27, 2009

How to check whether SPList exists in Sharepoint?

Sometimes we require to check whether SPList exists or not. There is no direct method to check for it. Instead we have to use try-catch block to catch the exception and to determine the list existence:

The code is:

using (SPSite ospSite = new SPSite("http://<servername>:<portname>"))
using (SPWeb ospWeb = ospSite.OpenWeb())
SPList ospList = ospWeb.Lists["ListName"];
if (ospList != null)
return true;
catch(Exception ex)
return false;

This code block creates an instance of SPList object. If ospList object is not null that is list is there so it returns true.
If list does not exists, an exception will be raised and false will be returned from catch block

Tuesday, April 21, 2009

Using SPPropertybag to save value

this small code snippet will explain how to store value to Sharepoint proerty bag and how to fetch it.

How to save value to SPProperty Bag
Here we will create an instanceof SPPropertyBag and try to add a key 'BlogImage' to the bag:

SPPropertyBag spProperties = SPContext.Current.Web.Properties;
if (spProperties.ContainsKey("BlogImage")) spProperties.Remove("BlogImage");
spProperties.Add("BlogImage", path);

NOTE: Updating SPPropertybag and SPWeb is important.

How to get value from SPProperty Bag

SPPropertyBag spProperties = ospWeb.Properties;
string blogImageValue=string.Empty;
if (spProperties["BlogImage"] != null && spProperties["BlogImage"] != "")
blogImageValue= spProperties["BlogImage"].Trim();

Wednesday, April 15, 2009

How to set People Picker value in Sharepoint

We all are aware of peoplepicker control.
Today we will discuss how to set its value from code and how to retrieve it.

Suppose we have a peoplepicker control named: pplowner

Setting Value to PeoplePicker from code:

pplOwner.CommaSeparatedAccounts = "EUNET\USERNAME";

If you wish to put the current logged in user name in peoplepicker, you can use:

pplOwner.CommaSeparatedAccounts =SPContext.Current.Web.CurrentUser.LoginName;

Fetching Value of PeoplePicker from code:

if (pplOwner.Accounts.Count > 0)
string userid= pplOwner.Accounts[0].ToString();

You can also iterate the usernames in peoplepicker as;

for (int i=0; i<pplowner.Accounts.Count;i++)
Console.Writeline( pplOwner.Accounts[i].ToString);

How to get current login SPUSer in sharepoint

Many times we require to get the logged in user in sharepoint.
We can easily get the SPUser object of the current logged in user:

SPUser user=SPContext.Current.Web.CurrentUser;

This will give the current logged in user. Now you can get user's email, groupName, Name from various properties like:
user.LoginName, user.Email etc

How to open SPSite, SPWeb in ASP.NET

This small article will describe , how to open SPSite and SPWeb from application and how to dispose it properly.

Since we are required to open a sharepoint object, so we need to refer Microsoft.Sharepoint.dll.
The location of the dll is:
Program Files\Common Files\Microsoft Shared\web server extensions\12\ISAPI

Step 1. : Refer the above dll and add the namespace:
using Microsoft.Sharepoint; //C# code written

Step 2:
SPSite is an object to create an instance of a Sharepoint Site
SPWeb is an object to create an instance of a Sharepoint Web.

using (SPSite ospSite=new SPSite ("http://servername:portname"))
using (SPWeb ospWeb=ospSite.openWeb())
//At this point you can perform any operation on ospWeb and ospSite.

Here, I have used using to open an instance of SPSite/SPWeb object because this automatically dispose the object as the scope ends.
You don't required to close and dispose it again.
For more detials, check the following link:

Friday, April 10, 2009

ASP.NET from Scratch - Code Behind - Part III

While our first example was fine, we unfortunately broke one of the coding principles of ASP.NET: To separate markup and code..

As you noticed, we added a scripting block (using <% %>), where we wrote a line of C# code to use the label. While this is just fine for a small and simple example like this, we would soon get a real mess with a bunch of C# code within an even bigger amount of HTML code.

If you throw in some JavaScript and some CSS as well, it will soon become very chaotic to edit. That's why MS introduced CodeBehind, a technique which allows you to completely separate markup (HTML, CSS etc.) and code (C#, VB.NET etc.). So let's remove the script block (from <% to %>) and save the file.

As we talked about earlier, VWD added a file called Default.aspx.cs. If you can't see it in the Solution Explorer, then click the little plus sign left of the Default.aspx file. Open this file. Now, if you haven't worked with .NET or another non-web programming language before, it might look a bit scary at this point. It looks nothing like HTML. However, I will try to explain the different parts of the content, and soon you will hopefully see that CodeBehind is a great tool to get a better overview of your work.

Here is a complete listing of the file as it looks right now:

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class _Default : System.Web.UI.Page
protected void Page_Load(object sender, EventArgs e)


For now, the top part is rather irrelevant to us. It's a list of namespaces being included with the using keyword, for usage in the file. Since this is an ASP.NET tutorial and not a dedicated C# tutorial, I won't explain this in depth out here. I will post a separate post for this topic.

Next, we have the class. Classes are a part of the concept of Object Oriented programming, which has become very popular, especially with languages like Java and C#. OO is a very complex subject, which also won't be explained within this tutorial.
The name of this class is "_Default", and the : (colon) tells us that this class inherits from the Page class in the System.Web.UI namespace. This means that our page can already do a bunch of things, without any programming, because it inherits methods and properties from another class.

All ASP.NET pages inherits from the Page class, or another class which inherits from the Page class.

The only method within this class is the Page_Load, which is called everytime the page is loaded. Let's use that to our advantage, and set the ext from this method. We can use the exact same line of code as before, but of course without the script block tags. Add the line of code between the { and } characters:

HelloWorldLabel.Text = "Hello, world!";

That's it. Run the project (F6), and have a look. The page looks exactly like before, but we have just used CodeBehind for the first time. But this example is starting to get a bit old, so in the next chapter, we will look into something a bit more interesting.

ASP.NET From Scratch - 1st Program - Part II

With MS Visual Web Developer installed, we're ready to create our first ASP.NET website. In VWD, this is very easy. Open the File menu and select "New Web Site". You will be presented with the following dialog:

You need to select "ASP.NET Web Site", if it's not already selected. You should also name your new site. This is done by entering a name in the Location box. This text box is probably already filled for you, with the last part being something like "Website1". You can choose to accept this, as well as the location of the project, or you can enter a new one, like I did. I have created a folder, "My Websites", and within this folder, I would like to create the new project with the name of "FirstWebSite". For now, this is less important, but later on you might wish to gather all your projects in a specific folder.

This tutorial will focus on the C# language. Once again, no knowledge of this is required, so if you already know another .NET language, you will get to learn some C# with this tutorial as well. Select C# in the Language dropdown. Now, click the Ok button to create this new website.

VWD will create a very basic website for you, consisting only of a Default.aspx file (and it's partner, the Default.aspx.cs file) and an App_Data folder. I will explain this later, but for now, just accept the fact that they are there. We will only use the Default.aspx for this first example. Move on to the next chapter, for the obligatory "Hello, world!" example.

Hello World Program

In almost every programming tutorial you will find the classic "Hello, world!" example, and who am I to break such a fine tradition? Let me show you how you can say hello to the world from ASP.NET. Open the Default.aspx (if it's not already opened) by doubleclicking it in the Solution Explorer. It already contains a bunch of (X)HTML markup, as well as some stuff you probably won't recognize, like the Page directive in the top, or the runat attribute on the form tag. This will all be explained later, but for now, we want to see some working code.

First of all, we will add a Label control to the page.
A Label control is some what simple, since it's just used to hold a piece of text. Add the following piece of HTML-looking code somewhere between the set of <form> tags:

<asp:Label runat="server" id="HelloWorldLabel"></asp:Label>

Secondly, add this script block somewhere on the page, preferably below the Page directive in the top:

HelloWorldLabel.Text = "Hello, world!";

If you haven't worked with ASP.NET before, I'm sure there's a bunch of things that you're wondering about now, but as I said, this is all about seeing some results right now.
To see the page in action, use Debug -> Start Without Debugging, or simply press F6. VWD will now compile your project, and launch the page you're working on in your default browser. The page will simply have a piece of text which says "Hello, world!" - congratulations, you have just created your first ASP.NET website!

Here is the complete listing:

HelloWorldLabel.Text = "Hello, world!";
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">
<html xmlns="" >
<head runat="server">
<title>Untitled Page</title>
<form id="form1" runat="server">
<asp:Label runat="server" id="HelloWorldLabel"></asp:Label>

ASP.NET From Scratch - Introduction - Part I

Welcome to this ASP.NET tutorial. According to Microsoft, "ASP.NET is a technology for building powerful, dynamic Web applications and is part of the .NET Framework". This tutorial will teach you ASP.NET from scratch, and no knowledge of any kind of server side scripting is required. Basic HTML and CSS knowledge is preferable. Having worked with Classic ASP or PHP won't give you much of an advantage, since ASP.NET is a whole new way of doing things.

.NET is language independent, which means you can use any .NET supported language to make .NET applications. The most common languages for writing ASP.NET applications are C# and VB.NET. While VB.NET is directly based VB (Visual Basic), C# was introduced together with the .NET framework, and is therefore a some what new language. Some people call C# "the .NET language", but according to Microsoft, you can do all the same things, no matter if you're using C# or VB.NET. The 2 languages are not that different, and if you have used one of them, you will have no problems at all learning the other. In this tutorial we will use C#.

One of the main differences between ASP.NET and Classic ASP/PHP is the fact that ASP.NET is compiled, while Classic ASP is always interpreted. PHP can be compiled by using commercial products, but usually it's interpreted as well.

This tutorial will use the free Visual Web Developer 2005 IDE from Microsoft. Despite what some people think, ASP.NET can be used without an IDE. It would be perfectly possible to write ASP.NET code in Notepad, and use the commandline compiler included with the .NET framework. Some people might actually prefer this "back to basics" way of doing things, but I believe that ASP.NET is best programmed with a decent IDE. You can use an IDE from another vendor, and you will still be able to follow most of this tutorial. We will use VWD 2005 since it's free, quick to install and easy to use. Besides, using a good IDE will make a lot of things faster in the long run.

Now, you can read more about VWD 2005 on this page: and download it from this page:


. The initial download is very small, because the installation is based on which components you already have and which will have to be downloaded. Just execute the file, and you will be guided through the installation. Once installed, start up the application, and proceed to the next chapter.

Thursday, April 9, 2009

How Google Interpret your query

This article describes how Google treats your search terms.

All Search Terms Count

Google returns only pages that match all your search terms.

For example a search for PR PLAN 2009 find pages containing the words “PR” and “PLAN” and “2009”.

This means Google adds implicit AND to the search query terms.

NOTE: Google sometimes returns pages that does not contains your query terms because Google returns pages in which your query terms are included in the link text to another page or place on the page.

Google also searches the metadata values set for a page (if any) with the query terms and if match found, the page is returned in search result.

Search Terms Match Exactly

Google returns pages that match your search terms exactly.

That means that Google simply matches string of characters together by putting and in between. It does not look for synonyms of words.

If you search for …Google won't find …
cheap inexpensive
effects influences
children kids

Stop Words

Some common words such as ‘the’, ‘a’, ‘an’, ‘on’, ‘where’, ‘this’, ‘is’, ‘when’ generally don’t add meaning to a search and are called Stop Words.

These words are omitted while performing the search.

For example if you search for ‘What to do for a student visa’, Google searches for terms ‘to’, ‘do’, ‘student’ and ‘visa’ ignoring ‘What’, ‘for’ , ‘a’.

Terms in Order
Google gives higher priority to pages that have the terms in the same order as in your query.

Consequently, you should enter search terms in the order in which you would expect to find them on the pages you're seeking. A search for [Microsoft latest technologies] gives priority to pages about Microsoft’s Latest Technologies.

Not Case-Sensitive

Google is not case-sensitive. It shows both upper- and lowercase results.
Ignoring case distinctions increases the number of results Google finds. A search for “Times Square” finds pages containing “Times Square”, “times square” or “TIMES Square”

Characters Ignored
Google ignores some punctuation and special characters, including

! ? , . ; [ ] @ / # < > .

For example Dr. Watson returns the same values as Dr Watson


A term with an apostrophe doesn’t match the term without an apostrophe.

For example, ‘we’er’ matches “we’er” but not “were” and vice versa.

Quoted Phrases

To search for a phrase, a proper name, or a set of words in a specific order, put them in double quotes.

A query with terms in quotes finds pages containing the exact quoted phrase.

For example, search term “White House” finds pages containing the phrase “White House” exactly.

NOTE: If you put stop words in between double quotes, Google will search for the phrase including those stop words.