close

Learn Spanish Valparaiso

On SolidQ blogs we have talked sometimes about working with XSLT in Sharepoint 2010, manipulating XML structures and the combination of both technologies to generate HTML. It is very important to learn how to work with these tools in Sharepoint 2010, since many Web Parts by default allow the personalization combining these technologies. This is the case of Content Query Web Part, Data Form Web Part and so on, that as we know, they are suitable for some tasks but not such as good for another ones. Could it be possible to generate a Web Part to carry out a very specific task that we need, and make it capable of personalizing at our discretion using XSLT? The answer is yes, so let’s see how. Firstly, we are going to observe the list of ingredients: We need a Sharepoint project, a Web Part type. We develop the running of the Web Part. We need a XML structure. A XSLT document. After seeing the ingredients, we are going to see the steps to follow: Once we have created the Web Part project inthe Solution we are working on, we will need to give it some logic (the Web Part must do something…). In this case, we are going to make a simple Web Part that consults a pictures library to generate a column of pictures as a banner. This is a very basic task, which can be used as an example in order to do more difficult tasks afterwards. The result is the following one: Firstly, we need to generate an XML to convert it later using an XSLT document: Private Function BuildXML() As XDocument Dim xNode As XElement Dim xListNodes As New List(Of XElement) 'We access to the list Dim oList As SPList = SPContext.Current.Site.RootWeb.GetList(lista) 'We go through the items of the list For Each item As SPListItem In oList.Items xNodo = _ <Foto titulo=<%= item.Title %> url=<%= item.Url %>/> xListNodes(xNodo) Next Dim XMLFinal As XDocument = _<?xml version="1.0" encoding="utf-8"?> <Fotos> <%= xListNodes %> </Fotos> Return XMLFinal End Function We create a list of nodes (xListNodes), which are elements of an XML document. For each element of the Sharepoint list, we create a node and then we add it to the named list of nodes and later we embed this list, with all its nodes, in the body of the variable XMLFinal, which will be the final XML document. Now that we have the XML we have to convert it in HTML, by an XSLT file. This XSLT file, we have saved it in the Sharepoint Style Library. The steps to follow are: first, we access to the document XSLT and then using XslCompiledTransform of .NET, which is the XSLT 1.0 processor to convert the file. Private Sub ApplyXSLT() Dim transform As New XslCompiledTransform() 'We built the absolute URL of the XSLT file DimficXsltUrl = SPContext.Current.Site.RootWeb.Url & ficXslt ' We create a WebClient instance Dim miWebClient As New WebClient() miWebClient.Credentials = CredentialCache.DefaultCredentials ServicePointManager.ServerCertificateValidationCallback = New System.Net.Security.RemoteCertificateValidationCallback(AddressOf CustomXCertificateValidation) ' We download the XSLT file using stream Dim myStream As Stream = miWebClient.OpenRead(ficXsltUrl) Dim sr As New StreamReader(myStream) ' We analize and we load the XSLT file Dim xr As New XmlTextReader(myStream) transform.Load(xr) Dim sWriter As New StringWriter() Dim writer As New HtmlTextWriter(sWriter) ' We convert the strem obtained, in the response of the petition Using reader As XmlReader = docXml.CreateReader Dim results As New HtmlTextWriter(writer.InnerWriter) ' The secondparameter that now it is nothing ' has the purpose of proportionate more parameters to the XSLT transform.Transform(reader, Nothing, results) reader.Close() End Using div.Controls.Add(New LiteralControl(sWriter.ToString())) ' We close the stream myStream.Close() End Sub It is worth mentioning that method Transform of the XslCompliedTransform, allow to proportionate as arguments parameters that will be used from the XSLT document. It is necessary to execute the previously mentioned function with high privileges, in order to not having permission problems with the anonymous user. Private Function ShowXML() As HtmlControls.HtmlGenericControl If (Not String.IsNullOrEmpty(ficXslt)) Then SPSecurity.RunWithElevatedPrivileges(AddressOf ApplyXSLT) End If Return div End Function Finally, with the method CreateChildControls, we use the following logic: Protected Overrides SubCreateChildControls() ' We built the XML docXml = FormarXML() 'XSLT XML = HTML div = MostrarXML() Me.Controls.Add(div) End Sub Previously we have to state the following global variables: ' Full path of the list that we are using Dim lista As String = "/SiteCollectionImages/fotoscol" ' Variable where we are saving the XML Dim docXml As XDocument ' Full path of the XSLT file Dim ficXslt As String = "/Style Library/XSL Style Sheets/FotosCol.xsl" Dim div As New HtmlControls.HtmlGenericControl("div") Where we are indicating the address of the Sharepoint list we are going to use, as well as the XSLT document. And that’s all buddies, I hope it is helpful

Cristian has more than three years of experience as SharePoint Developer. Cristian is a former SolidQ team member. Latest posts by Cristian Zaragoza () - December 13, 2013 - June 1, 2012 - June 20, 2011

learn spanish 6000 words     learn 5000 spanish words

TAGS

CATEGORIES