See Apps in Action
Build Your Own Apps
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.
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();
}
}
}
using System.ComponentModel; using System.Data; using System.Collections.Generic;
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; }
}
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;
}
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;
}
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:
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:
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="" />
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.

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.






The code is long but very useful, thank u.
Joseph