Search This Blog

Monday, September 8, 2014

SharePoint WEb services Code && Cache Code and Som Concept

SPSiteDataQuery class is used to "retrieve data from selected lists or from all lists in the current site collection."

SPContext Object

When building custom web parts for Windows SharePoint Services V3. You can use the 
SPContext object to get for instance the site or web from the current context.You can also get
 the listitem and the webfeatures.

In WSS V2 you would use SPControl.GetContextSite() or SPControl.GetContextWeb() but that's 
a lot less flexible and it's slower as well.
A couple of different uses of SPContext are shown below:

*************************************************************************
SPList currentList = SPContext.Current.List;
SPWeb currentSite = SPContext.Current.Web;
SPSite currentSiteCollection = SPContext.Current.Site;
SPWebApplication currentWebApplication = SPContext.Current.Site.WebApplication;

*************************************************************************

SPListItem item = (SPListItem)SPContext.Current.Item;

*************************************************************************

SPWeb site = SPContext.Current.Site.OpenWeb(guid);
SPUser user = SPContext.Current.Web.CurrentUser;

*************************************************************************

SPSiteDataQuery siteQuery = new SPSiteDataQuery();
DataTable queryResults = SPContext.Current.Web.GetSiteData(siteQuery);
queryResults.TableName = "queryTable";
queryResults.WriteXml("C:\\queryTable.xml");

The last example makes use of the SPSiteDataQuery object.  In WSS V3 there are
 two different query objects. Besides SPSiteDataQuery object there is also the SPQuery object.
The difference between the two is that SPQuery can only be used for queries within a single 

folder in a single list. SPSiteDataQuery can be used for cross lists and even cross site queries.




Basic Query - Query to get all the Items from a list where Category field is equal to "Sp2007"

// 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 (suggested using U2U Query Bulider Tool)
curQry.Query = " SP2007 ";

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

//Get the List 
SPList myList = myWeb.Lists["ListName"]; 

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

// Go through the resulting items 
foreach (SPListItem curItem in curItems) 
{
string ResultItemTitle = curItem["Title"].ToString();
}

Query on DateTime Field - Query to Find Items in the List with Today's date.

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


//Write the query 
DateFieldQuery.Query = “+ DateTime.Now.ToString("yyyy-MM-ddTHH:\\:mm \\:ssZ") + ”;

//Get the List 
SPList myList = myWeb.Lists["ListName"]; 

//Get the Items using Query
SPListItemCollection ResultItems = myList.GetItems(DateFieldQuery); 
Query Using Yes\No Columns - 
Query to Retrieve all the Items from a list where a 

Yes\NO type Field, named "AreYouCool?" is "Yes".


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


//Write the query 
CheckBoxQuery .Query = “1 ”;

//Get the List 
SPList myList = myWeb.Lists["ListName"]; 

//Get the Items using Query
SPListItemCollection ResultItems = myList.GetItems(CheckBoxQuery);

SharePoint Server Publishing features


The publishing features that are enabled when the SharePoint Server Publishing feature is enabled on a nonpublishing site.

In Site settings - When you enable the SharePoint Server Publishing feature, the following 

changes are made to the Site Settings page:

* In the Galleries section, the Master pages link is removed, and it is replaced with the 

Master pages and page layouts link at both the site collection and site level.

* In the Site Administration section, a Site output cache link is added at the site level only.

* In the Look and Feel section, the following links are added at both the site collection and site level:

o Master Page

o Page layouts and site templates

o Welcome Page

* In the Site Actions section, the Save site as template link is removed at both 

the site collection and site level.

* In the Site Collection Administration section, the following links are added at 

the site collection level only:

o Site collection cache profiles

o Site collection object cache

o Site collection output cache

Regional settings- When you enable the SharePoint Server Publishing feature, 

the Subsite Settings section is added to the Regional Settings page. This enables 
you specify whether all sites below the current site should inherit the regional settings 
set for the current site.


Document libraries and lists Created

When you enable the SharePoint Server Publishing feature, the following document 

libraries and lists are added:

* Documents This library stores documents that are used on pages in the site.

* Images This library stores images that are used on pages in the site.

* Pages This library stores pages that are created in the site.

* Workflow Tasks This list stores workflow tasks that are created in the site.

List and Libraries Settings-
 In addition to the libraries and lists that are created, 

the following changes are made to document library settings:

* On the Document Library Settings page, in the General Settings section, 

a Manage item scheduling link is added.

* On the Versioning Settings page, the following changes are made:

o The Document Version History option is set to Create major and minor (draft) versions. 

This option determines what versions are created when a file is edited in the Pages library.

o The Draft Item Security option is set to Only users who can edit items.

 This option determines who can see draft items in the Pages library.

o The Require Check Out option is set to Yes. This option requires documents to be checked

 out before they can be edited.
Navigation

The following navigation changes are made when you enable the SharePoint Server Publishing Infrastructure feature:

* The top link bar is replaced with the global navigation menu.

* Default settings for the global navigation menu and the Quick Launch menu are specified.

Web Parts

When you enable the SharePoint Server Publishing Infrastructure feature, the following 

Web Parts are added at the site collection level and are available to use in all sites
 that are created within the site collection:

* Content Query Web Part

* Media Web Part

* Summary Links Web Part

* Table Of Contents Web Part


Page editing menu

When you enable the SharePoint Server Publishing feature, the following 

changes are made to the page editing menu:

* The Publish tab and Publish button are added to the master page.


Other changes


When you enable the SharePoint Server Publishing feature, the following changes are made:

* Users can no longer create pages that have a space in the name. 

Spaces are automatically converted to a dash ‘-‘.

* The Manage Content and Structure link is added to the Site Actions menu.

 This opens the Site Content and Structure tool for the entire site collection. 

SharePoint Web services Samples



//Add a New List Item
protected void CreateListItem(string ID, string Title)
{

SpWebservice.Lists SPService = new SpWebservice.Lists();
SPService.Credentials = System.Net.CredentialCache.DefaultCredentials;

System.Xml.XmlNode ndListView = SPService.GetListAndView("MyList", "");
string strListID = ndListView.ChildNodes[0].Attributes["Name"].Value;
string strViewID = ndListView.ChildNodes[1].Attributes["Name"].Value;

XmlDocument doc = new XmlDocument();

System.Xml.XmlElement batch = doc.CreateElement("Batch");

batch.SetAttribute("OnError", "Continue");

batch.SetAttribute("ListVersion", "1");

batch.SetAttribute("ViewName", strViewID);

batch.InnerXml = " " +
"" + ID + "" + Title+ "";
try
{
SPService.UpdateListItems(strListID, batch);
}
catch { }

}

//Update list Item 

protected void UpdateListItem(string ListID,string Title)
{
SpWebservice.Lists SPService = new SpWebservice.Lists();
SPService.Credentials = System.Net.CredentialCache.DefaultCredentials;

System.Xml.XmlNode ndListView = SPService.GetListAndView("MyList", "");
string strListID = ndListView.ChildNodes[0].Attributes["Name"].Value;
string strViewID = ndListView.ChildNodes[1].Attributes["Name"].Value;

XmlDocument doc = new XmlDocument();

System.Xml.XmlElement batch = doc.CreateElement("Batch");

batch.SetAttribute("OnError", "Continue");

batch.SetAttribute("ListVersion", "1");

batch.SetAttribute("ViewName", strViewID);

batch.InnerXml = "" + ListID+ "" + Title + "";

try
{
SPService.UpdateListItems(strListID, batch);
}
catch { }

}

//Get List Items 

protected void GetListItems(string ID)
{

SpWebservice.Lists SPService = new SpWebservice.Lists();
SPService.Credentials = System.Net.CredentialCache.DefaultCredentials;

System.Xml.XmlNode ndListView = SPService.GetListAndView("MyList", "");
string strListID = ndListView.ChildNodes[0].Attributes["Name"].Value;
string strViewID = ndListView.ChildNodes[1].Attributes["Name"].Value;

XmlDocument xmlDoc = new System.Xml.XmlDocument();

XmlNode ndQuery = xmlDoc.CreateNode(XmlNodeType.Element, "Query", "");

XmlNode ndViewFields = xmlDoc.CreateNode(XmlNodeType.Element, "ViewFields", "");

ndViewFields.InnerXml = "";

ndQuery.InnerXml = "" + ID +"";

try
{
XmlNode ndListItems =
SPService.GetListItems(strListID, null, ndQuery, ndViewFields, null, null, null);

XmlDocument doc = new XmlDocument();
doc.LoadXml(ndListItems.OuterXml);
XmlNamespaceManager mg = new XmlNamespaceManager(doc.NameTable);
mg.AddNamespace("sp", "http://schemas.microsoft.com/sharepoint/soap/");
mg.AddNamespace("z", "#RowsetSchema");
mg.AddNamespace("rs", "urn:schemas-microsoft-com:rowset");
mg.AddNamespace("y", "http://schemas.microsoft.com/sharepoint/soap/ois");
mg.AddNamespace("w", "http://schemas.microsoft.com/WebPart/v2");
mg.AddNamespace("d", "http://schemas.microsoft.com/sharepoint/soap/directory");

XmlNodeList NodeList = doc.SelectNodes("//sp:listitems/rs:data", mg);

foreach (XmlNode ListItem in NodeList)
{
foreach (XmlNode node in ListItem.ChildNodes)
{
string ID = string.Empty;
string Title = string.Empty;

XmlAttribute id = node.Attributes["ows_ID"];

if (id != null)
{
ID = id.Value;
}

XmlAttribute _Title= node.Attributes["ows_Title"];

if (_Title!= null)
{
Title= _Title.Value;
}

_AddToDataTable(ID, Title);
}
}
catch { }

}

// Get list Fields (using GetList method)

Below is the code to get the values of a choice field in a sharepoint list . 
You can save these values into a datatable and can bind the datatable with a Drop-down control.

//get choice field values from sharepoint list 

protected void GetDrpDownValuesFromList()
{
SPWebservice.Lists FMService = new SPWebservice.Lists();
FMService.Credentials = System.Net.CredentialCache.DefaultCredentials;
XmlNode ndLists = FMService.GetList("MyList"); //add list name
XmlDocument doc = new XmlDocument();
doc.LoadXml(ndLists.OuterXml);

XmlNamespaceManager mg = new XmlNamespaceManager(doc.NameTable);
mg.AddNamespace("sp", "http://schemas.microsoft.com/sharepoint/soap/");
mg.AddNamespace("z", "#RowsetSchema");
mg.AddNamespace("rs", "urn:schemas-microsoft-com:rowset");
mg.AddNamespace("y", "http://schemas.microsoft.com/sharepoint/soap/ois");
mg.AddNamespace("w", "http://schemas.microsoft.com/WebPart/v2");
mg.AddNamespace("d", "http://schemas.microsoft.com/sharepoint/soap/directory");

XmlNodeList FieldsInList = doc.SelectNodes("//sp:Field", mg);

foreach (XmlNode Field in FieldsInList)
{
if (Field.HasChildNodes)
{
if (Field.Attributes["Name"].Value == "choiceFieldName")
{
foreach (XmlNode node in Field.ChildNodes)
{
if (node.HasChildNodes)
{
foreach (XmlNode Newnode in node.ChildNodes)
{
if(Newnode.HasChildNodes)
{
_addToDataTable(Newnode.InnerText); // Add node value to datatable
}}}}}}}

SharePoint Interview Questions - 3


Web Parts are componentized, self-contained packages of user interface that 
can be dropped into place on SharePoint Web Part pages to provide discrete sets of functionality to users.
It can be incredibly easy to get confused between sites, webs, web applications, and 

site collections. The farm is the topmost level in the hierarchy. Below the farm, 
you have web applications represented by the SPWebApplication class, which typically
 correspond to an IIS application pool. Below that, you have a collection of site collections
 contained in the SPSiteCollection class. Finally, you have site collections represented by the 
SPSite class and individual websites represented by the SPWeb class.

Features allow reusable pieces of functionality to be created and deployed to other sites,without modifying

 site templates.It is always better to deploy a feature in new site instead ofdirectly embedding mountains of complex XML.Using Features, you can do everything from adding a link to the Site Settings page to creating
 a complete, fully functioning Project Management suite that can be added to any SharePoint site.Features 
are organized in folders under the Features directory located under 12 hives; Where SharePoint Server 2007
 puts 
all of its system files, at the following path: %SystemDrive%\Program Files\Common Files\Microsoft
 Shared\web server extensions\12. The two files that are used to define a feature are the feature.xml and Elements.xml .The feature XML file defines the actual feature and will make SharePoint aware of the
 installed feature. It usually identifies the Feature itself and its element manifest file and sets the Feature 
scope to 
Web site.
Elements.xml file identifies the assembly, class, and method to implement in feature.

You can directly deploy a feature in sharepoint site with

stsadm -o installfeature -filename XYZEventHandler\Feature.xml

stsadm -o activatefeature -filename DeletingEventHandler\Feature.xml -url href="http://server/Site/Subsite">http://Server/Site/Subsite

iisreset

OR To Deploy it as solution package you need a solution manifest (manifest.xml).


Solutions allow you to package Features in a cabinet (.cab) file and define important metadata about 

those Features. After a Solution is installed on a server in the farm, you can then use SharePoint’s 
Solution
 management features to automate the deployment of that Solution to other sites within the farm.

The solution manifest (always called manifest.xml) is stored at the root of a solution file. 

This file defines the list of features, site definitions, resource files, Web Part files, and assemblies
 to process. It does not define the file structure—if files are included in a solution but not listed in the 
manifest XML file, they are not
 processed in any way.

Because the solution file is essentially a .cab file, use the makecab.exe tool to create the solution

 package. The makecab.exe tool takes a pointer to a .ddf file, which describes the structure of the .cab file. 
The format of a .ddf file is, declare a standard header and then enumerate, one file per line, the set of files
 by where they live on disk, separated by where they should live in the .cab file.


Features & Solutions:
The Feature Framework has been extended to allow developers to create custom Features. Features can 

be deployed by using SharePoint Portal Server 2007 new form of deployment, namely Solution Deployment. Solutions as you know, are custom packages (e.g. WSP file) or redistributable CAB files, created by
 developers and deployed by SharePoint Administrators. Administrator can deploy Features to the individual 
site or to all Web front End Servers.

Features are a method for developers to package customisations and deploy them to the SharePoint portal. 

They can then be activated and deactivated at the Site Collection level. Solutions are a way to 
bundle features together for deployment.


Custom action : Represents a link, toolbar button, menu item, or any control that can be added to a toolbar

 or menu that appears in the UI. You define custom actions by using a custom action element
 within a feature definition file. You can bind custom actions to a list type, content type, file type, 
or programmatic identifier
 (ProgID). For more information, see Custom Action Definitions.


Event receiver: Evaluator of an event and definer of the behavior of an application. Windows SharePoint 

Services 3.0 allows you to define event handlers within libraries, lists, and sites. Event receivers can be 
defined by using a receiver element within a feature definition file. For more information, see Event 
Registrations.


Master page: 
Pages that provide a consistent layout and appearance (look and feel) for SharePoint sites.

 They allow you to factor out layout, structure, and interface elements such as headers, footers, navigation 
bars, and content placeholders. Master pages in ASP.NET 2.0 and master pages in Windows SharePoint 
Services work in the same way. For more information, see Building Simple Master Pages for Windows
 SharePoint Services 3.0.


Module :
 A file or collection of file instances that define the location where the files are installed during site creation. Modules are frequently used to implement a Web Part Page in the site. 

You can define modules by using a module element within a feature definition file. For more information, see Modules.


SharePoint site:
 A Web site hosted in a virtual URL. A SharePoint site is a place for collaboration, communication, or content storage. Depending on your business needs, you can create sites such as

 team sites, blog sites, wiki sites, and others. You can customize a site's appearance, users, user
 permissions, galleries, and site administration by using the Site Settings administration pages.


SharePoint site collection:
 A collection of SharePoint sites that share common administration 

pages and site settings. Site collections allow you to share content types, site columns, templates,
 and Web Parts within a group of SharePoint sites.


SharePoint Web farm:
 A group of Office SharePoint 2007 servers that share the same 

configuration database. All site content and all configuration data is shared for all front-end
 Web servers in a server farm.


Site definition.:
 A set of files that includes a master XML configuration file that is stored 

on all front-end Web servers. A site definition provides the basic blueprint for how sites look, 
what lists they include, their default navigational structures, and so on. For more information,
 see Working with Site Templates and Definitions.


Theme: 
A group of files (CSS, images) that allow you to define the appearance (look and feel) 

of Web pages. Themes in ASP.NET 2.0 and themes in SharePoint Products and Technologies 
work in the same way. Themes are used to help organizations to brand their portals and team sites. 
Office SharePoint Server 2007 includes a set of predefined themes. However, as a developer, 
you can create custom themes for your company.

No comments:

Post a Comment