Search This Blog

Wednesday, June 12, 2013

Wednesday, June 5, 2013

How to delete data from Sharepoint List using API?
Normally once you delete the data from the list it goes to Recycle Bin. During reading SDK I came to know about a method 'Delete' which deletes data permenently. does not send it recycle bin. So now we have two different approaches to deleting the list data.

Approach1 - Delete Data Permanently:

using(SPSite siteCollection = new SPSite("http://portal"))
{
    using(SPWeb site = new siteCollection.Openweb())
    {
        SPList listdata = site.Lists["MyList"];
        for(int i=listdata.Items.Count-1;i>=0;i--)
        {
            listdata.Items.delete(i);
        }
    }
}

Approach2 - Delete Data and it goes to Recyclebin:

using(SPSite sitecollection = new SPSite("http://portal"))
{
    using(SPweb site = sitecollection.Openweb())
    {  
    SPList listdata = site.Lists["MyList"];
    for(int i=listdata.Items.Count-1;i>=0;i--)
    {
    SPListItem olistitem = listdata.Items[i];
    olistitem.Recycle();
    }
    }
}

SharePoint Interview Questions and Answers: Basics

My next series of posts will be related to SharePoint Interview questions and answers. This will help most of you guys. What I am gonna do is - Simply take any topic in SharePoint and provide the list of questions and answers asked by the interviewer. I have some good amount of experience in interviewing the people, cause in my career many time I was on the interviewer's chair than interviewee.

Lets start with the SharePoint Basics:

1. What is Ghosting?
- page ghosting is an optimization used with site pages in which a single page template can be used to process multiple page instances across many different site. A page template, such as default.aspx, is compiled into an assembly dynamic-link library (DLL) and loaded into memory just once per Web application. When a user customizes a site page by using the SharePoint Designer and then saves those changes, a customized version of the page definition is stored in the content database.

2. SPVirtualPathProvider
- WSS stores storing customized versions of .aspx files and .master files inside the content database and retrieving them on demand when they are needed to process an incoming page request.
SPVirtualPathProvider is able to retrieve an ASP.NET page file from the content database, such as default.aspx, and then pass it along to the ASP.NET page parser.

3. Difference between Site pages and Application pages.
- Pages that support user customization are known as site pages. home page (default.aspx) is a site page. It supports user customization by using tools such as the SharePoint Designer.
Application page does not support customization. The standard Site Settings page (settings.aspx) is a good example of an application page. It can be accessed from any site, yet it does not support customization.
Application pages are deployed as physical files on the file system of the front-end Web server in a directory at the following path:
SharePoint 2007: c:\program files\common files\microsoft shared\
web server extensions\12\TEMPLATE\LAYOUTS {12 hive}

SharePoint 2010: c:\program files\common files\microsoft shared\
web server extensions\14\TEMPLATE\LAYOUTS 
{14 hive}
4. Why SafeControls entry required in web.config in SharePoint
- It determines what controls a user might place on a customized page. For example, imagine a scenario in which a site administrator tries to mount an attack by adding a server-side control to a site page and parameterizing it in a certain way. Safe mode allows the farm administrator to determine which controls can be used in pages that are processed in safe mode.

5. What are the Featutes?
- Features provide a mechanism for defining site elements and adding them to a target site or site collection through a process known as feature activation. The element types that can be defined by a feature include menu commands, link commands, page templates, page instances, list definitions, list instances, event handlers, and workflows. A feature consists of a directory created within a special WSS system directory:

c:\program files\common files\microsoft shared\web server extensions\12\TEMPLATE\Features
The directory for a feature contains one or more XML-based files that contain Collaborative Application Markup Language (CAML). Each feature directory contains a manifest file named feature.xml that defines the high-level attributes of the feature, such as its Id, Title, Description, Version, Scope, Hidden and ImageUrl. It also contains one or more additional XML files (elements.xml) that define the actual elements that make up the feature.
Feature Scopes:
The Scope defines the context in which the feature can be activated and deactivated. The feature we are creating has a scope equal to Web, which means it can be activated and deactivated within the context of the site. If you assign a Scope value of Site, your feature will then be activated and deactivated within the scope of a site collection. The two other possible scopes for defining a feature are WebApplication scope and Farm scope.
Feature EventReceivers:
Create a class that inherits from the SPFeatureReceiver class. As you can see, you handle events by overriding virtual methods in the base class such as FeatureActivated and FeatureDeactivating. There are also two other event handlers that fire when a feature is FeatureInstalled or FeatureUninstalled

6. What is site column?
- A site column is a reusable column definition that can be used across multiple lists. A site column defines the name for a column, its underlying field type, and other characteristics such as the default value, formatting, and validation. Advantage is that you can update the site column in a single place and have that update affect all the lists where the site column has been used.

7. What is Content Type?
- A content type is a flexible and reusable WSS type definition that defines the columns and behavior for an item in a list or a document in a document library.
You can also existing list columns to the list content types on the list. Also when you add columns directly to a list, and that list contains multiple content types, you can specify whether you want the new column to be referenced in all the content types on that list.

8. SharePoint Lists.
- At the heart of the core WSS architecture is the infrastructure for defining list types and provisioning list instances to store content. Document libraries, which play a vital role in creating WSS business solutions.
Following are the List Types. I have highlited some list types, which are majorly used in SharePoint Development:
List Type
Description
Document library
Used for collaborating on documents with support for versioning, check-in and check-out, and workflow. Includes support for deep integration with Microsoft Office.
Form library
Used to store XML documents and forms for use with Microsoft Office InfoPath.
Wiki page library
Used for collaborative Web pages based on wiki pages, which are dynamically generated and collaboratively edited Web pages.
Picture library
A specialized document library enhanced for use with pictures. Includes support for slide shows, thumbnails, and simple editing through Microsoft Office Picture Manager.
Announcements
Used for simple sharing of timely news with support for expiration.
Contacts
A list for tracking people and contact information, with support for integration into Microsoft Office Outlook and other WSS-compatible contacts applications.
Discussions
A simple list for threaded discussions with support for approval and managing discussion threads.
Links
A list for managing hyperlinks.
Calendar
A list for tracking upcoming events and deadlines. Includes support for integration and synchronization with Office Outlook.
Tasks
A list of activity-based items that can integrate with workflow.
Project tasks
An enhanced tasks list with support for Gannt chart rendering and integration with Microsoft Office Project.
Issue tracking
A list for tracking issues and resolution, with support for prioritization.
Custom list
An empty list definition for extending with custom columns, or created using Microsoft Office Excel spreadsheets.

SharePoint Object model code to access any list:
SPList list = null;
using (SPSite site = new SPSite("http://siteurl"))
{
      using (SPWeb web = site.OpenWeb())
{
string listName = "SampleList";
list = web.Lists[listName];
// list = web.GetList("/Lists/ SampleList"); This method is // useful, as no code change required when the list Title // changes.

SPListItem newItem = list.Items.Add();
newItem ["Title"] = "First Item";
newItem.Update();  
// You can use newItem.SystemUpdate(); if you don’t want  // update modified date and version.
}
}

To get specific results within a list, you can use the SPQuery object as follows: 
query.ViewFields = @"<FieldRef Name='Title'/><FieldRef Name='Expires'/>";query.Query =@"<Where><Lt><FieldRef Name='Expires' /><Value Type='DateTime'><Today /></Value></Lt></Where>";
SPList list = site.Lists["SampleList"];
SPListItemCollection items = list.GetItems(query); 
SPSiteDataQuery class can return items from many different lists through an entire site collection. hence queries run with the SPSiteDataQuery class are sometimes referred to as cross-site queries.

Tuesday, June 4, 2013

SharePoint 2013 CAML Query Designer - It's not only CAML query builder... More than that...

Karine Bosch and Andy van Steenbergen created new caml query designer. We can download it frombiwug site by clicking on downloads and select caml designer.
SharePoint 2013 caml designer provides many more features including all features of SharePoint 2010 caml query builder with nice look and feel. Here is some screen shots with explanation.
Download the caml builder into your Dev server, select the .exe file and run as administrator.


CAML designer 2013 contains language interoperability. By default it is set to English. On the top left we can see two options CAML and Site Data Query to get queries. In the Top of the right side we can see Connection link and settings link with Refresh, Twitter follow us, Help.



By clicking on the settings link, we can see two tabs to set general and display settings as shown in the images below.



In the general settings, it allows us to clear the history and language settings. In display settings we have the option to set the background colors and lighting view.
By clicking on the connection link, we can see connection types (Server OM, Client OM, Web Services) and SharePoint version (SharePoint 2013, SharePoint 2010, Office 365) and the authentication credentials (Current user or allows us to enter the credentials) as shown in the image below.


For SharePoint 2010, we can see only Client OM, Web services options. (I think caml designer is detecting the current installed SharePoint version. So that it is not showing Server OM for SharePoint 2010).



For SharePoint 2013, we can see Server OM, Client OM, web services options as shown below.



For Office 365, we have Client OM and Web services options.


Here I have selected SharePoint 2013, Server Om and current User credentials. By clicking on the connect button, it will connects the SharePoint site. While connecting SharePoint site shows the message “Trying to connect….” As shown in the image below.

After connecting the site, it shows all the lists and libraries on the site as shows in the image.


Select a list for caml query; we have drag and options for view fields as shown in the image below.


Drag and drop the fields to see in the results.

In the “Order By” tab select the field to show in the order.


In the “Where” tab, select the options or logic by dragging the fields and setting the logic.


“Query Options” tab allows us to select the options.


We can results finally in “Test” tab for all these customization.


On the bottom window we can see CAML Query for customized logic. We can also see  Server OM code, .Net Client Object model code, Rest Client Object Model code as well as Web services. As shown in the images below.


In the Rest Services we have two options, JSON, ATOM as per the required result type.

You can see more explanation on CAML Query designer here