Translate

Tuesday, August 21, 2012

SharePoint Content Types

Content Type

Content Types are the building blocks of site columns and content types defines the schema of the fields of list item ,Content types helps in reusing the site columns across the site collection, we can create content types and attach these

How to navigate Content Type page

Content types to site columns to create content type navigate to site settings page and click on site content types.

To view the list of available content types of a site collection you can directly navigate to the page "mngctype.aspx"

Content Type scope

Content types can be created based on site collection or inside child site of a site collection

The following shows the lists Ids associated with each type of list/templates of the list

Parent
Name
Id
Item
Document
0x0101
Item
Announcement
0x0104
Item
Task
0x0108
System
Item
0x01
Item
Link
0x0105
Item
Contact
0x0106
Item
Event
0x0102
Folder
Summary Task
0x012004
Document
Picture
0x010102
Folder
Discussion
0x012002
Document
Form
0x010101
Document
Wiki Page
0x010108
Document
Basic Page
0x010109
Document
Master Page
0x010105



Using AvailableContentTypes

If you want to know the avaible content type using code you can use "AvailableContentTypes" SPWeb site = SPContext.Current.Web; // iterate content types for current site and parent sites too foreach (SPContentType myctype in site.AvailableContentTypes) { }

Create content type programmatically

The following code demonstrate how to create content type programmatically

Sample code:
//access the web of the site collection
SPWeb site = SPContext.Current.Site.RootWeb;
//create content type name
string myctypeName = "hey my content test";
//access parent
SPContentType myctypeParent = site.ContentTypes["Item"];
//create content type
SPContentType myctype = new SPContentType(myctypeParent, site.ContentTypes, myctypeName);
//assigned the description and create a group
myctype.Description = "I am doing content type programatically";
myctype.Group = "My Content Type Group";
//Add to site collection
site.ContentTypes.Add(myctype);

Monday, August 20, 2012

Querying Multiple Lists using SPSiteDataQuery

SPSiteDataQuery



SPSiteDataQuery is used to query multiple lists and SPQuery fetches only from single list
Sample code demonstrate
a query that returns events from all calendars
in the current site collection where the end date is later than Current Date

Sample Code:

SPSiteDataQuery query = new SPSiteDataQuery;

query.Query = "<Where><Gt><FieldRef Name='EndDate'/>" +
"<Value Type='DateTime'><Today OffsetDays=\"-1\"/></Value></Gt>
</Where>";

query.Lists = "<Lists ServerTemplate='106' />";
query.ViewFields ="<FieldRef Name='Title' />" ;
query.Webs = @"<Webs Scope='SiteCollection' />";
DataTable table = SPContext.Current.Site.RootWeb.GetSiteData(query);


Working with CAML Queries and SPQuery

CAML Queries



In sharepoint if we want to retrieve the data from Lists we need to use CAML Queries
CAML stands for Collaberative application markup language,which is basically xml format language query style.

To query the list we need to use SPQuery object by creating new object and assigned the CAML query to SPQuery.Query

CAML Query operators

Operator                Description
And                 It will be used to combined the two or more conditions or multiple conditions
BeginsWith      Searches for a string at the beginning of the text field
Contains         Searches for a string within the text field
Eq                Equal to
FieldRef         A reference to a field (useful for GroupBy elements)
Geq              Greater than or equal to
GroupBy       Groups results by these fields
Gt                 Greater than
IsNotNull      Is not null (not empty)
IsNull            Is null (empty)
Join               Used to query across two lists that are joined through a Lookup field
Leq               Less than or equal to
Lt                  Less than
Neq             Not equal to
Now            The current date and time
Or               Boolean or operator
OrderBy     Orders the results of the query
Today         Today’s date
TodayIso    Today’s date in International Organization for Standardization (ISO) format
Where       Used to specify the Where clause of the query 




the following example will list out all the announcements which are not expired till date,we will be fetching all announcements "Title"

Code Sample:
SPQuery query = new SPQuery;
query.Viewfields = @"<fieldref name="Title"><fieldref name="Expires">";
query.Query =
@"<where>
<lt>
<fieldref name="Expires">
<value type="DateTime"><today></today></value>
</fieldref></lt>
</where>";

SPList list = SPContext.Current.Web.Lists.TryGetList("Announcements");
SPListItemCollections items = list.GetItems(query);

Here "items" are announcements title result</fieldref></fieldref>

Sunday, August 19, 2012

Using TryGetList in sharepoint 2010

TryGetList in sharepoint 2010


How to get the list directly without looping through SPWeb List collection
There is a Provision in SharePoint 2010 "TryGetList" actually will give the list by passing the list name

Here is sample to demonstrate the "TryGetList"

            SPSite site = new SPSite(SPContext.Current.Web.Url);
            SPWeb currentWeb = site.RootWeb;

            SPList customList = currentWeb.Lists.TryGetList("MyCustomList");

Creating Event Handlers in SharePoint 2010

Using Visual Studio 2010 Creating sharepoint Event Handlers

Sharepoint Event Handlers


There are two types of event receivers are available in sharepoint<br />


1.Synchronous
Synchronous event receiver is also know as before event receivenrs
These events are fired before doing any operations on list
For example
ItemAdding is one event 
ItemDeleting is one more event which is performed on a List Item

2.Asynchronous
Asynchronous events are those which happen after any operation is being performed on list item
For example
ItemAdded,ItemDeleted
actually these are events used for business flow purpose
for example if you want to send an email to the user whenever new item is added to the list


We can implement event receiver for the following
1.List Events
2.List Item Events
3.List Email Events
4.Web Events
5.List Workflow Events
6.Feature Events

 
Steps to create List Item Events receivers

Step 1.Open Visual studio 2010

Step 2.Select the event receiver template as shown in the following figure


Step 3.Select the select which you want to bind the event receiver

Step 4.Visual studio will ask to select which event you need to add as shown in the following figure


Step 5.Click on Finish



I am using a sample of event receiver where i am not allowing the user to delete any item from the list


using Microsoft.SharePoint.Saecurity;
using Microsoft.SharePoint.Utilities;
using Microsoft.SharePoint.Workflow;

namespace EventHandlersSample

{
    public class SimpleDeleteEventHandler : SPItemEventReceiver    {

public override void ItemDeleting(SPItemEventProperties properties)
{
properties.Cancel = true;
    properties.ErrorMessage = "Deleting is not authorized.";

   
}
    }
}

Step 6.Once code is done just press F5 the event receiver will be attached the selected list and you debug

In visual studio 2010 by default it creates a feature for the event handler but if you are using visual studio 2008 we need to create the feature manually

In SharePoint 2010 there is a provision to add our own custom message page instead of using SharePoint default error page.


Friday, December 23, 2011

MCTS Books for Guide: Exam 70-667 and 70-573 And 70-576

Mcts Microsoft Sharepoint 2010 Configuration Study Guide: Exam 70-667 (With CD)

(Paperback)
by

James Pyles cost about

Rs. 617
See link Books

Sharepoint 2010 Developer's Certification: Certification Toolkit For Exams

70-573 And 70-576

(Paperback)
See link Books

Sharepoint Interview Questions Books

I Found one book on sharepoint interview Questions see link
Sharepoint interview Questions