Mashups and Microsoft SharePoint Part 1.4.1: List Mashlets Web Part, using Presto Connector for C#

Posted 03/17/2009 - 14:50 by jeremy.pitten

0
Your rating: None

Mashups and Microsoft SharePoint Part 1.4.1: Creating a List Mashlets Web Part using Presto Connector for C#

This is the fifth in a many-part series about Mashups and Microsoft SharePoint.  You can read about the entire series at http://www.jackbe.com/enterprise-mashup/blog/mashing-sharepoint-introduction.

Whereas the MashletView Web Part, described in the previous article, provides a web part viewer in which to render a Mashlet, this second Web Part will make use of the Presto Connector for C# and ASP.NET UI Controls to access the Mashup Server and render the Web Part UI.

We will keep things as simple as possible in order to focus on the process of building and deploying the Web Part.

We will create Web Part which uses Presto Connector for C# (PC4C#) to retrieve a list of available Mashlets from Presto Mashup Server and display the list using a System.data.GridView UI Control.

Create MashletList Web part

  1. We can either create a new project or add this new Web Part to the Jackbe.Samples Assembly, created in the previous article for the MashletView Web Part. We'll assume the same project is used.
  2. Download and install the PC4C#, instructions can be found here, Installing the Presto Connect for C# Library.
  3. The Web Part will be dependent on 2 Assemblies bundled with the PC4C#:
    • Jackbe.PrestoConnect.Net.dll
    • NetServ.Net.Json.dll
  4. Add References to these 2 assemblies.
  5. Create a new Web Part named MashletsList and reference the PC4C# assemblies:

using System;
using System.Runtime.InteropServices;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Serialization;

using JackBe.Presto.Connect.Net;
using NetServ.Net.Json;

namespace Jackbe.Sample
{
    [Guid("5d77828a-4a62-428e-871e-93e740365d36")]
    public class MashletsListView : System.Web.UI.WebControls.WebParts.WebPart
    {
        public MashletsListView()
        {
        }

        protected override void CreateChildControls()
        {
            base.CreateChildControls();
        }
    }
}
  1. The following additional namespace statements will be required:
using System.ComponentModel;
using System.Data;
using System.Collections.Generic;
  1. As with the previous Mashlet, we will define Web Part properties so we can configure which Presto Mashup Server the Web Part will connect to. We also define properties to hold user credentials which may be required to access Presto Services.
        private string _Hostname;
        private string _Port;
        private string _Username;
        private string _Password;

        [WebBrowsable(true)]
        [WebDisplayName("Hostname")]
        [WebDescription("Presto Hostname")]
        [Personalizable(PersonalizationScope.Shared)]
        public String Hostname
        {
            get {     return _Hostname;   }
            set {    _Hostname = value;   }
        }

        [WebBrowsable(true)]
        [WebDisplayName("Port")]
        [WebDescription("Presto Port number")]
        [Personalizable(PersonalizationScope.Shared)]
        public string Port
        {
            get {   return _Port;   }
            set {  _Port = value;   }
        }

        [WebBrowsable(true)]
        [WebDisplayName("User Name")]
        [WebDescription("Presto User Name")]
        [Personalizable(PersonalizationScope.Shared)]
        public string Username
        {
            get { return _Username; }
            set { _Username = value; }
        }

        [WebBrowsable(true)]
        [WebDisplayName("Password")]
        [WebDescription("Presto User Password")]
        [Personalizable(PersonalizationScope.Shared)]
        [PasswordPropertyText(true)]
        public string Password
        {
            get { return _Password; }
            set { _Password = value; }
        }

  1. The following GetMashletsDataSet method will connect to the Presto Mashlet Server and retrieve a summary list of all available Mashlets. This Mashlet summary data is then used to populate a DataSet object which is returned by the method.
        private DataSet GetMashletsDataSet()
        {
            DataSet dataSet = new DataSet();
            DataTable table = new DataTable();

            table.Columns.Add("Name");
            table.Columns.Add("Title");
            table.Columns.Add("Description");
            table.Columns.Add("Type");
            table.Columns.Add("Created");
            table.Columns.Add("Owner");

            Connection connection = this.GetConnection();
            JsonObject response = this.GetMashlets(connection);

            JsonObject mashletMap = (JsonObject)response["map"];

            foreach (KeyValuePair<string, IJsonType> pair in mashletMap)
            {
                string name = pair.Key;
                JsonObject config = (JsonObject)mashletMap[name];

                string title = config["title"].ToString();
                string description = config["description"].ToString();
                string type = config["type"].ToString();
                string created = config["created"].ToString();
                string owner = config["createdBy"].ToString();

                table.Rows.Add(new Object[] { name, title, description, type, created, owner });
            }

            dataSet.Tables.Add(table);
            connection.Logout();
            return dataSet;
        }

        private Connection GetConnection()
        {
            Connection connection = new Connection();
            connection.HostName = Hostname;
            connection.PortNo = Int32.Parse(Port);
            connection.Login(Username, Password);
            return connection;
        }

        private JsonObject GetMashlets(Connection connection)
        {
            JumpResponse response = connection.Invoke("MashletHub", "getMashlets");
            JsonObject json = response.ResponseBody.Json;
            return json;
        }

  1. The final part of the implementation is to add the CreateChildControls method, to retreive the DataSet of Mashlet details and bind it to a GridView UI Control.
        private GridView MashletsView;
        private DataSet MashletsDataSet;

        protected override void CreateChildControls()
        {
            base.CreateChildControls();
            if (String.IsNullOrEmpty(Hostname))
            {
                Label label = new Label();
                label.Text = "Web Part not configured";
                Controls.Add(label);
                return;
            }

            MashletsView = new GridView();
            MashletsView.AllowSorting = true;
            MashletsView.AllowPaging = true;
            MashletsView.PageSize = 10;
            MashletsView.EnableSortingAndPagingCallbacks = true;
            MashletsView.Sorting += new GridViewSortEventHandler(MashletsView_Sorting);
            MashletsView.PageIndexChanging += new GridViewPageEventHandler(MashletsView_PageIndexChanging);

            MashletsDataSet = GetMashletsDataSet();
            MashletsView.DataSource = MashletsDataSet;

            Controls.Add(MashletsView);
            MashletsView.DataBind();
        }
        protected void MashletsView_Sorting(object sender, GridViewSortEventArgs e)
        {
            MashletsDataSet.Tables[0].DefaultView.Sort = e.SortExpression;
            MashletsView.DataBind();
        }
        protected void MashletsView_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            MashletsView.PageIndex = e.NewPageIndex;
        }

Deploying the MashletsListView Web Part

We follow a similar deployment procedure to Mashlet View Web part, except in this case we also have a few dependent Assemblies to deploy and we need to configure the SharePoint server to allow the Web Part to access Sharepoint Server Web Resources, see Microsoft Windows SharePoint Services and Code Access Security for more details.

The 3 Assemblies to deploy are:

  1. Jackbe.Sample.dll, our Web Part implementation(s)
  2. Jackbe.PrestoConnect.Net.dll, the Presto Connection for C# assembly
  3. NetServ.Net.Json.dll, a lightweight library for handling JSON notation written in C#

The Assemblies should be copied to:

C:\Inetpub\wwwroot\wss\VirtualDirectories\port number\bin

Add a Safe Control entry in web.config for the new Web Part:

<SafeControl
    Assembly="Jackbe.Samples, Version=1.0.0.0, Culture=nuetral, PublicKeyToken=null"
    namespace="Jackbe.Samples"
    TypeName="MashletsListView"
    Safe="True"
    AllowRemoteDesigner="True"
/>

Finally we need to configure the SharePoint Server to allow the Web Part to connect to the Presto Mashup Server. We have 2 options, one simple one not so simple:

  • The simple option is to update web.config so that the default trust level is set to Full, so that all Web Parts have full access to SharePoint resources. This is just a matter of locating the trust element in web.config and replacing the default trust level, WSS_Minimal setting to Full:
<trust level="Full" originUrl="" />

Creating a Custom Security Policy File

Rather than applying a Full trust level we will create a custom security policy which extends the standard SharePoint WSS_Minimal trust level.

Navigate to C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\config\, the default location for SharePoint security policy files, and create a copy of of WSS_MinimalTrust.config, name it something like Presto_WSS_MinimalTrust.config

Open Presto_WSS_MinimalTrust.config and create a copy of the PermissionSet named SPRestricted, rename the copy as Presto and append a new IPermissionSet element which defines a WebPermission setting:

<PermissionSet class="NamedPermissionSet" version="1" Name="Presto">
     <IPermission class="AspNetHostingPermission"
         version="1"
         Level="Minimal"
     />
     <IPermission class="SecurityPermission"
         version="1"
         Flags="Execution"
     />
     <IPermission class="WebPartPermission"
         version="1"
         Connections="True"
     />
     <IPermission class="SharePointPermission"
         version="1"
         ObjectModel="True"
     />
     <IPermission class="WebPermission"
         version="1"
         Unrestricted="True"
     />
</PermissionSet>

Insert, as the first child, a new CodeGroup element which uses a UrlMembershipCondition to reference all the Jackbe.*.dlls in bin:

<CodeGroup
      class="UnionCodeGroup"
      version="1"
      PermissionSetName="Presto">
      <IMembershipCondition
            class="UrlMembershipCondition"
            version="1"
            Url="$AppDirUrl$/bin/Jackbe*dll"
      />
</CodeGroup>

Having saved the updated config file, we must now update web.config to reference this file. Locate the SecurityPolicy element and add a the following new trustLevel child element:

<trustLevel
     name="Presto_WSS_Minimal"
     policyFile="C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\12\config\Presto_WSS_MinimalTrust.config"
/>

Finally, we must reset the server so that it picks up this config change. Open a console window and enter the command iisreset.

Now we are ready to add the new Web Part to the SharePoint Web Part gallery.

Add the MashletListView Web Part to a SharePoint Web Part Page

  1. From your SharePoint Site homepage, select Site Actions->Site Settings
  2. In Site Settings, select Web Parts from the Galleries column, which will display the Web Part Gallery.
  3. Select *New to add a new Web Part, this will display a list of all available New Web Parts.
  4. Select the checkbox for the Jackbe.Sample.MashletsListView and click the Populate Gallery button, this will return you to the Web Part Gallery where you should see the new Web part listed as MashletsListView.webPart!NEW.
  5. The mashlet can now be added to any Web Part page. We could change the mashlet settings so that it appears in a new Grouping, by default it will appear under Miscellaneous Web Parts.

Rendering the MashletsListView Web Part

  1. Navigate to the Web Part page where you want to add the MashletsListView Web Part, select Site Actions-> Edit Page.
  2. Click Add a Web Part in the zone you wish to add the Web Part, this should pop up the Add Web Parts dialog where you should find the new MashletsListView Web Part listed in the Miscellaneous section. Select the checkbox and click Add, this should add the web Part to the current page.
  3. Select Web Part Edit-> Modify Shared Web Part to configure the mashlet properties. The screenshot below shows the unconfigured Web Part in Edit Mode.

  1. Having configured the hostname, port, username and password properties, apply and save these setting. The screenshot below shows the final rendered Mashlet.

 

 

This post is part of a many-part series about Mashups and Microsoft SharePoint.  You can read about the entire series at http://www.jackbe.com/enterprise-mashup/blog/mashing-sharepoint-introduction.