Sunday, 9 June 2013

Page Processing Model

As with Microsoft Content Management Server 2002, Microsoft Office SharePoint Server 2007 is a template-based page rendering system. The two fundamental templates used for page rendering in SharePoint sites are master pages and page layouts. Master pages can be used by all page instances in a site. Page layouts can be used by all page instances that are based on that page layout. Page layouts are stored as list items in the Master Page and Page Layout Gallery, so you can use versioning, check-in and check-out, workflow, and other features available for SharePoint document libraries.
Office SharePoint Server 2007 is built on top of Windows SharePoint Services 3.0 and Microsoft ASP.NET 2.0. The ASP.NET engine interprets and runs all SharePoint page requests. For example, suppose a user requests a page named welcome.aspx in the browser. The ASP.NET engine retrieves the page layout associated with the page and the master page associated with the site via the Windows SharePoint Services 3.0 FileProvider object, and it renders the field controls and Web Parts from the fields onto the page. The following figure shows the page processing model in Office SharePoint Server 2007.
The page processing model in MOSS

The Page Processing Model

The following list gives a step-by-step breakdown of how ASP.NET 2.0 and Windows SharePoint Services 3.0 interact to render pages in a SharePoint site:
  1. The browser requests a Web page from Microsoft Internet Information Services (IIS).
  2. IIS passes the request to ASP.NET 2.0.
  3. An HttpApplication pipeline is created for the request.
  4. ASP.NET 2.0 fetches the page via the Windows SharePoint Services 3.03 file provider. ASP.NET passes the URL to the file provider, and the file provider fetches the page and returns the page stream. The Windows SharePoint Services file provider implements caching and reduces round-trips to the database.
  5. ASP.NET loads a Page class, parses the page stream, and finds a reference to the page layout upon which the page is based.
  6. The ASP.NET engine compiles the page stream and stores it in memory.
  7. ASP.NET queries the Windows SharePoint Services file provider for the page layout.
  8. ASP.NET loads the stream for the page layout associated with the current page.
  9. ASP.NET compiles the page layout and stores it in memory. ASP.NET can free this memory later if the system needs memory.
  10. ASP.NET determines the master page for the site and fetches the master page via the Windows SharePoint Services file provider.
  11. ASP.NET compiles the master page and writes to the disk so you never have to recompile the master page unless you modify it.
  12. The page layout runs each control on the page in the context of the page that was requested.
  13. ASP.NET updates the necessary caches.
  14. IIS returns the page to the browser.
The next time the page is requested by the same user or by a different user who has the same permissions to see the page as the first user, page processing is much more efficient:
  1. The browser requests a Web page from IIS.
  2. IIS passes the request to ASP.NET 2.0.
  3. An HTTPApplication pipeline is created for the request and hits the HandleRequest.
  4. ASP.NET uses all the internal caches.
  5. ASP.NET renders the HTML for the controls.
  6. IIS returns the page to the browser.



    When a site visitor requests a page, SharePoint combines content and templates from the site to render the fi nal page. SharePoint begins by retrieving the requested page from the Page library. This provides SharePoint with the metadata that identifi es the web page, as well as the content that the author entered into the page. Next, SharePoint retrieves the Master Page and the Layout page from the Master Pages and Page Layouts gallery. SharePoint determines the correct page layout by
    examining the content type of the requested web page. Finally, SharePoint combines the Master Page, Layout page, and content into a rendered HTML page that it sends back to the end user ’ s browser.
    There is an important relationship between the templates and content that is defi ned by content types. The content type is a container for the site columns that defi ne the content and metadata for the web pages. As the container for this information, the content type acts as the glue between the content and the templates. The content type is assigned to both the template and the Pages library. This allows the template to use placeholders that align to the site columns, and it allows the author to use the template within the site.

Create managed metadata column using feature

Create managed metadata column using feature

I have seen lots of blogs which talks about creating managed metadata column via feature but I haven’t got a single article which provided me a complete solution. In this blog, I am going to talk on creating managed metadata column using feature and assigning it a to proper term set.
Click here to download the sourecode.
Pre-requisites.
Create a Managed metadata column with the below settings.
  •  Group Name: Electronics
  • Term set: Computer
  • Term:  Dell, Samsung, HP
Below is the screen shot after creating the column.






 Creating Column via Feature.
1.       Open Visual Studio and create an Empty SharePoint Project(  I have named as ManagedMetadata_Column)
2.       Add a New Item. Select Empty Element.

 3. Paste the below section in between the elements tab. 
  <Field DisplayName="MMSingleSelect"
       ID="{D069F181-5674-4D87-BDA8-2AC3CE6F6E39}"
       Type="TaxonomyFieldType"
       DisplaceOnUpgrade="TRUE"
       Required="FALSE"
       Group="Simple Rule Site Columns"
       SourceID="http://schemas.microsoft.com/sharepoint/v3"
       StaticName="MMSingleSelect"
       Name="MMSingleSelect"
       Overwrite="TRUE">
  </Field>
  <Field DisplayName=" MMMultiSelect"
         ID="{B1D07B1A-60B9-40A9-A53C-48BAB6FABD23}"
         Type="TaxonomyFieldTypeMulti"
         DisplaceOnUpgrade="TRUE"
         Required="TRUE"
         Mult="TRUE"
         Group="Simple Rule Site Columns"
         SourceID="http://schemas.microsoft.com/sharepoint/v3"
         StaticName="MMMultiSelect"
         Name="MMMultiSelect"
         Overwrite="TRUE">
  </Field>

4. Above section adds 2 columns with Multi Select enabled and another one with Multi Select disabled.
Click on the feature and click on the Add event Receiver. This will add new cs class. Uncomment the FeatureActivated method and paste the below code. Basically, this code will read the field and will update the column values. This code is self-explanatory



public class Managed_Metadata_ColumnEventReceiver : SPFeatureReceiver
{
public const string METADATA_SERVICE = "Managed Metadata Service";
public const string MMSingleSelect = "MMSingleSelect";
public const string MMMultiSelect = "MMMultiSelect";
public const string ATTRIBUTE_GROUP = "Electronics";
public const string TERMSET = "Computer";
public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
SPWeb currentWeb = null;
XmlDocument fieldDefinition = null;
XPathNavigator fieldNavigator = null;
XPathNodeIterator selectedNodes = null;
try
{
// feature is scoped at Site, so the parent is type SPSite rather than SPWeb..
using (SPSite site = properties.Feature.Parent as SPSite)
{
if (site != null)
{
currentWeb = site.RootWeb;
}
using (currentWeb)
{
string listName = string.Empty;
string columnName = string.Empty;
string groupName = string.Empty;
string finalCaml = string.Empty;
string hiddenListId = string.Empty;
string webId = string.Empty;
fieldDefinition = new XmlDocument();
fieldDefinition.LoadXml(currentWeb.Fields.SchemaXml);
fieldNavigator = fieldDefinition.CreateNavigator();
fieldNavigator.MoveToRoot();
selectedNodes = fieldNavigator.Select("//Field[@Type='TaxonomyField'] | //Field[@Type='TaxonomyFieldType'] | //Field[@Type='FilteredLookupTextField']| //Field[@Type='TaxonomyFieldTypeMulti']");
while (selectedNodes.MoveNext())
{
groupName = selectedNodes.Current.GetAttribute("Group"string.Empty);
if (groupName == "Simple Rule Site Columns")
{
TaxonomySession session = new Microsoft.SharePoint.Taxonomy.TaxonomySession(site);
TermStore termstore = session.TermStores[METADATA_SERVICE];
columnName = selectedNodes.Current.GetAttribute("Name"string.Empty);
TaxonomyField taxonomyColumn = currentWeb.Fields.GetFieldByInternalName(columnName) as TaxonomyField;
currentWeb.AllowUnsafeUpdates = true;
switch (columnName)
{
case MMSingleSelect:
SetTaxonomyTermSet(termstore, taxonomyColumn, session, ATTRIBUTE_GROUP, TERMSET);
break;
case MMMultiSelect:
SetTaxonomyTermSet(termstore, taxonomyColumn, session, ATTRIBUTE_GROUP, TERMSET);
break;
}
}
currentWeb.AllowUnsafeUpdates = false;
}
}
}
}
catch (Exception ex)
{
//
}
}
private void SetTaxonomyTermSet(TermStore termstore, TaxonomyField taxonomyColumn, TaxonomySession session,string termStoreGroup, string termSet)
{
Group group = termstore.Groups[termStoreGroup];
TermSet termset = group.TermSets[termSet];
taxonomyColumn.SspId = termstore.Id;
taxonomyColumn.TermSetId = termset.Id;
taxonomyColumn.Update();
}
}

 
5. Once the WSP is deployed and go activate the feature ManagedMetadata_Column Feature. You should see 2 column created under "Simple Rule Site Columns".
Click here to download the sourecode.

Thursday, 14 March 2013

Rendering XML using XSL – HTML Output


Overview
Some times we need to display the XML data in our web application in specific format. XSLT provides the ability to display the XML document in some specific format like HTML, PDF etc. We can select a a XML file or a portion of XML File and using XSL Transformation we can display in some specific format.
SampleFlow
An XSL transformation need an XML document to transform and an XSL style sheet describing how the transformation will take place. An XSLT engine then transforms the XML document via the XSL style sheet and will produce the output in the format specified by the style sheet.
Here I am just going to show you how we can display a XML data using XSL in our web page and which will help beginners to start with. This is an sample application. The XSL, which I have used over here is very simple. If you want to learn details on XSL please read tutorials from W3School.

How to Implement ?

1. Create Data Base :
Rather than reading the data from xml, I have read the data from database. First of all I have create an DB Student with table name “StudentDetails” . Table contain some dummy data like,
db
2. Add XSL File
Before, reading the data from database, we have to create the XSL file, We can add XSL file by just right click on the project Add New Item>Select XSLT File
AddXSL
I have put the xsl file in a specific folder called XSL .

FileH

3. Desing XSL
Now, Designing XSL is one of the important task, and there are many things that related with XSL . In my case, this is very simple XSL, but if you need to learn in details, I will suggest you to read from W3School. First of all have a look into the XML data which I have got from the dataset.
xml
And based on that we need to desing the XSL File. Below is the StudentDetails XSL



<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

 <xsl:template match="/">

<table width="100%" align="center" cellpadding="0" cellspacing="0" border="1"

 style="background-color:#123456;font-family:verdana;font-size:10pt;border:1">

<tr>

<td  width="10%"  align="left" >

 Roll</td>

<td width="70%" align="left">

 Name</td>

<td width="20%" align="left">
 Address</td>

</tr>

</table>

<xsl:for-each select="Students/Table">

<table width="100%" align="center" cellpadding="0" cellspacing="0" border="1"
 style="font-family:verdana;font-size:10pt;border:1">

<tr >

<td  width="10%"  align="left"   >

 <xsl:value-of select="Roll"/></td>

<td width="70%" align="left" >

 <xsl:value-of select="Name"/></td>

<td  width="20%" align="left" >

 <xsl:value-of select="Address"/></td>

</tr>

</table>

</xsl:for-each>

 </xsl:template>

</xsl:stylesheet>


Now, have a look into the code,
Read the data from database and put it into dataset. We can easily get the XML from dataset using.
1string XMLString=ds.GetXml();
Below code is used to read data from database

public string strstudentDetails = string.Empty;

 protected void Page_Load(object sender, EventArgs e)

 {

 string    _strConnectionString = "Data Source=.\\sqlexpress;Initial Catalog=Student;Integrated Security=True";

 string _strquery = "select * from studentDetails";

 SqlConnection con = new SqlConnection(_strConnectionString);

 DataSet ds = new DataSet("Students");

 SqlDataAdapter da = new SqlDataAdapter(_strquery, con);

 da.Fill(ds);

 //Get the XML From DataSet

 string strXML = ds.GetXml();

 strstudentDetails=GetHtml(Server.MapPath("~/xsl/studentDetails.xsl"), strXML);

 }
GetHtml function actually doing the job. Its taking XSL Stylesheet and XML data as parameter and returning the html output


 /// <summary>

 /// Get HTML From XML and XSL

 /// </summary>

 ///

<param name="xsltPath">XSL File Path</param>

 ///

<param name="xml">XML String</param>

 /// <returns>HTML Output</returns>

 public static string GetHtml(string xsltPath, string xml)

 {

 MemoryStream stream = new MemoryStream(ASCIIEncoding.Default.GetBytes(xml));

 XPathDocument document = new XPathDocument(stream);

 StringWriter writer = new StringWriter();

 XslCompiledTransform transform = new XslCompiledTransform();

 transform.Load(xsltPath);

 transform.Transform(document, null, writer);

 return writer.ToString();

 }

 }
Now for displaying the result, we have to put following line in the aspx page,

<head runat="server">

 <title>Student Page</title>

</head>

<body>

 <form id="form1" runat="server">

<div>

<table >

<tr>

<td>

 <b>Student Info : Displying using XSL Rendering</b></td>

</tr>

<tr>

<td>

 <%= strstudentDetails %></td>

</tr>

</table>

</div>

</form>

</body>

</html>
and the output like,
Output123
Hope this will help you to move ahead with XSL Transformation.
Thankyou