Sunday, December 27, 2009

Creating a Basic Event Handler

Please go through the following MSDN link.

Link: http://msdn.microsoft.com/en-us/library/ms437502.aspx

Working with List Objects and Collections in SharePoint

To perform actions on list data in a SharePoint Web site, you must first obtain an SPWeb object to serve as an entry point to the object model, allowing you to access lists, items, documents, users, alerts, etc.

Objects
Most classes in the Microsoft.SharePoint and Microsoft.SharePoint.Administration namespaces start with SP. Generally, classes in the Microsoft.SharePoint assembly that don't start with this prefix represent Web form controls.
Windows SharePoint Services typically does not save modifications of object properties to the database until you call the Update method on the given object. The following example shows how to change the title and description for the Tasks list.

Sample Code:
SPList myList = myWeb.Lists["Tasks"];
myList.Title="New_Title";
myList.Description="List_Description";
myList.Update();

Collections
Just as lists are at the center of a SharePoint site, so are collections at the center of its object models. You can use each collection to add, delete, enumerate, and update a type of object. Collection classes generally share the following traits:
Has a name that ends in "Collection."
Implements the System.Collections.ICollection interface.
Has a Count property of type Int32.
Has an Int32 indexer that can be used to get the nth item in the collection.
Has an indexer that takes an item identifier.
Has Add and Delete methods.


Calling the Add method for a collection usually updates the database on the back-end server with the appropriate data, except when additional information is required in order to update data. In this case, using the Add method returns an object that you can use to gather the information. For example, to add an item to a list, first use the Add method of the Microsoft.SharePoint.SPListItemCollection class to return an SPListItem object, assign values to appropriate properties of the object, and then call the Update method to effect changes within the content database.

Indexers
Indexers provide a useful means to access individual items in collections. To return an item, use square brackets ([]) in C# to contain either an index or a string that identifies the item within the collection.
For example, if mySite represents an instance of the SPWeb class, then SPList myList = mySite.Lists["Announcements"] returns the Announcements list in C#.
To return only a subset of items from the list, you can call the list object's GetItems method and pass an SPQuery object to specify the subset:
SPListItemCollection myItems = myList.GetItems(myQuery)

You can use an indexer to return a specific field from a list (for example, myList.Items["Field_Name"] in C#. You can also specify a field name in an indexer and iterate through the collection of items in a list in order to return values from the field. This example displays the Due Date, Status, and Title values for each item in a list:

Sample Code
foreach(SPListItem myItem in myItems)
{
Response.Write(SPEncode.HtmlEncode(myItem["Due Date"].ToString()) + "
");
Response.Write(SPEncode.HtmlEncode(myItem["Status"].ToString()) + "
");
Response.Write(SPEncode.HtmlEncode(myItem["Title"].ToString()) + "
");
}

The next example shows how to return all Title and Status values for the Tasks list on a SharePoint Web site.
SPWeb mySite = SPContext.Current.Web;
SPList myTasks = mySite.Lists["Tasks"];SPListItemCollection myItems=myTasks.Items;
foreach(SPListItem myItem in myItems)
{
Response.Write(SPEncode.HtmlEncode(myItem["Title"].ToString()) + " :: " + SPEncode.HtmlEncode(myItem["Status"].ToString()) + "
");
}

You can also use indexers to modify values in a list. In the following example, a new list item is added to a collection and the values for a URL column are assigned to the item:
SPListItem myNewItem = myList.Items.Add();
myNewItem["URL_Field_Name"] ="URL, Field_Description";
myNewItem.Update();

The following example sets the Status and Title values for a list item.
SPListItem myItem = myItems[0];
myItem["Status"]="Task_Status";
myItem["Title"]="Task_Title";
myItem.Update();

An indexer throws an ArgumentOutOfRange exception if it does not find the specified item.
[Courtsey MSDN]

Thursday, December 24, 2009

Friday, December 4, 2009

For Beginners: Add a custom ASP.NET page in your SharePoint Application

1. You have created your SharePoint site --> http://ServerName:PortNumber
2. You have created your .Aspx page in Visual Studio --> MyPage.aspx. (Remember to provide Complete details in 'Inherit' field of the 'Page' attribute present in the start of the .aspx page including Fully qualified page name, namespace, version, culture and public key token)
3. You have compiled the application and placed the error free dll in the GAC.
3. Go to 12 Hives --> C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\TEMPLATE\FEATURES
4. Create a new folder named MyPageFeature and copy & add an existing feature structure there. Then add a new entry for your page i.e., MyPage.aspx in the Elements.xml file and delete all the other old links.
5. now open command prompt and navigate to --> C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\BIN
6. write the following command --> stsadm -o installfeature -name MyPageFeature -force
7. After the installation of your feature, you need to activate it by the following command --> stsadm -o activatefeature -name MyPageFeature -url http://ServerName:PortNumber
8. After operations completed succesfully, close the command prompt.
9. Reset IIS or the respective Application Pool.
10. Write the complete url (Eg: http://ServerName:PortNumber/MyPageFeature/MyPage.aspx) and hit enter to find your new page running in SharePoint environment.