Tuesday, May 24, 2011

How to Customize RequestAccess Application Page – SharePoint 2010

In SharePoint Manage Access request option is used to allow users to request access to a Web site or a list. We can enable this option by navigating to Site Actions -> Site Permissions -> Manage Access Requests. In order to enable this option, we need to configure outgoing e-mail settings in the Central Admin under System Settings -> Configure outgoing e-mail settings.

Then users can request permission to the site using RequestAccess page. In SharePoint 2010, master pages are not applied to this application page. What we can do here is we can create a custom RequestAccess page with our own fields. Then we can use Set-SPCustomLayoutsPage Power Shell Command to map a new path for the custom RequestAccess layout page as follows:

Set-SPCustomLayoutsPage –Identity "RequestAccess" –RelativePath "/_layouts/custompages/MyRequestAccess.aspx" –WebApplication "http://server_name/mywebapp"




This is the implementation of MyRequestAccess custom page. It is implemented to support access requests for SP sites as well as SP lists.

public partial class MyRequestAccess : UnsecuredLayoutsPageBase
{
private string m_strListID;

protected void Page_Load(object sender, EventArgs e)
{
// Check whether the access request permission is enabled.
if (!base.Web.RequestAccessEnabled)
{
// Redirect to Error Page.
}
if (base.Request.QueryString["type"] == "list")
{
this.m_strListID = base.Request.QueryString["name"];
if (!IsGuid(this.m_strListID))
{
// Redirect to Error Page.
}
else
{
SPSecurity.CodeToRunElevated secureCode = null;
if (secureCode == null)
{
secureCode = delegate
{
using (SPSite site = new SPSite(
new Uri(base.Site.Url).ToString(), SPUserToken.SystemAccount))
{
using (SPWeb web = site.OpenWeb(base.Web.ServerRelativeUrl))
{
SPList list = web.Lists[new Guid(this.m_strListID)];
if (!list.RequestAccessEnabled)
{
// Redirect to Error Page.
}
}
}
};
}
SPSecurity.RunWithElevatedPrivileges(secureCode);
}
}
}

protected void btnSubmit_OnClick(object sender, EventArgs e)
{
// Check access request is for a list or the site.
bool isSite = this.m_strListID == null ? true: false;

try
{
string parentSiteWithUniquePermissionsUrl = SPUtility.GetFullUrl
(base.Web.Site, base.Web.FirstUniqueAncestorWeb.ServerRelativeUrl);
string siteUrl = SPUtility.GetFullUrl(base.Web.Site,
base.Web.ServerRelativeUrl);

// Read user povided values.
string reason = SPHttpUtility.HtmlEncode(txtReason.Text);
string agencyCode = SPHttpUtility.HtmlEncode(txtAgencyCode.Text);

string parentSiteWithUniquePermissionsLayoutsUrl =
String.Concat(parentSiteWithUniquePermissionsUrl, "/_layouts/");
string siteLayoutsUrl = String.Concat(siteUrl, "/_layouts/");

// Get user name.
string userName = string.Empty;
if (base.Site.WebApplication.GetIisSettingsWithFallback(
base.Site.Zone).UseClaimsAuthentication)
{
userName = SPClaimProviderManager.Local.GetUserIdentifierEncodedClaim(
this.Context.User.Identity);
}
else
{
userName = this.Context.User.Identity.Name;
}
string encodedUserName = SPHttpUtility.UrlKeyValueEncode(userName);

// Build access granting link.
string grantAccessLink = null;
if (isSite)
{
grantAccessLink = "<a href = \"" +
SPHttpUtility.UrlPathEncode(parentSiteWithUniquePermissionsLayoutsUrl
+ "aclinv.aspx", true) + "?users=" + encodedUserName + "\">";
}
else
{
grantAccessLink = "<a href = \"" +
SPHttpUtility.UrlPathEncode(siteLayoutsUrl + "aclinv.aspx", true) +
"?users=" + encodedUserName + "&obj=" +
SPHttpUtility.UrlKeyValueEncode(this.m_strListID) + ",list\">";
}

// Create message body "string messageBody" //

// Send Email.
return SPUtility.SendRequestAccessToOwner(base.Web, isSite ?
SPObjectType.Web : SPObjectType.List, this.m_strListID, messageBody);
}
catch (Exception)
{ }

// Redirect to confirmation page.
SPUtility.Redirect("confirmation.aspx?ConfirmationID=RequestAccessSent",
SPRedirectFlags.RelativeToLayoutsPage, this.Context);
}
}

Monday, May 16, 2011

Using SharePoint 2010 Data View Web Part

The Data View Web Part is all about aggregating and managing data from various data sources. They are able to retrieve data from various data sources and appearance of that data can be adjusted by applying Extensible Stylesheet Language Transformations (XSLT). Here are the steps to create a simple web part.
  1. Create a DVWP in SharePoint Designer 2010, consuming a data source (Ex: SP list)
  2. Customizations (HTML, CSS, XSLT)
  3. Export Web Part
  4. Include Web Part in a Visual Studio solution

Create a DVWP, consuming a SP List as the data source
  • Open SharePoint site in SPD and create an .aspx page ("All Files"->"Pages"->”New”->“ASPX”).
  • Right click new .aspx page and select "Edit File in Advanced Mode"
  • Before doing any changes, save the page. It will automatically add required references to the top of the page.
  • Then add an Empty Data View to the page.

  • Select "Click here to select a data source" option to select a data source.
  • From the “Data Source Picker”, select the interested List or Document Library
Selecting a data source will lists all the available fields in the right hand side, in the "Data Source Details" pane. Here we can select required fields to be displayed in the Web Part by holding Ctrl key. It should be noted that even though we didn’t select a field from data source details, we can use any available field for conditions checking, etc.

  • Once required fields are selected, expand "Insert Selected Fields as..." dropdown from the same pane. In here to have multiple items in the WebPart, select "Multiple Item View"

Now we are in a state of we can preview the Web Part control. Save changes and press F12 to see the page in browser.

In the above view we get all the records listed as a table. If required we can do the modifications like Grouping, Sorting, Paging, etc. For that select Web Part in the design view and go to "Options" tab. For grouping, select "Sort & Group" option.



Here I have sorted recodes by Start Time and also grouped records by Start Time.



Customizations (HTML, CSS, XSLT) and export

Refer data source by List Name instead of List ID
If we check the “SelectParameters” section of the Web Part code, we can see Data Source is referred using ListID. Here we can change it t List Name or List URL to add more flexibility.
<SelectParameters>
<WebPartPages:DataFormParameter Name="ListID" ParameterKey="ListID"
PropertyName="ParameterValues"
DefaultValue
="{3168E887-8820-4970-8A55-5D576879}"/>
<asp:Parameter Name="StartRowIndex" DefaultValue="0"/>
<asp:Parameter Name="nextpagedata" DefaultValue="0"/>
<asp:Parameter Name="MaximumRows" DefaultValue="10"/>
</SelectParameters>
First change "ListID" to "ListName" and give list name in the "DefaultValue" field in the SelectParameters section.
<SelectParameters>
<WebPartPages:DataFormParameter Name="ListName" ParameterKey="ListName"
PropertyName="ParameterValues" DefaultValue="Announcements"/>
<asp:Parameter Name="StartRowIndex" DefaultValue="0"/>
<asp:Parameter Name="nextpagedata" DefaultValue="0"/>
<asp:Parameter Name="MaximumRows" DefaultValue="10"/>
</SelectParameters>

Then go to "
<parameterbindings>" section and change "ListID" to "ListName"
and "DefaultValue" to name of the list.

<ParameterBinding Name="ListName" Location="None" DefaultValue="Announcements"/>


Adding Styles to the Control/Records
Find "" statement and add styles around "" part to apply styles to the whole control. It will work as a wrapper style for the Web Part surrounding all items.
<div class="ccccc">
<xsl:call-template name="dvt_1"/>
</div>

If need to add a wrapper styles around each list item separately add it around following section.
<xsl:call-template name="dvt_1.body">
<xsl:with-param name="Rows" select="$Rows"/>
<xsl:with-param name="FirstRow" select="1" />
<xsl:with-param name="LastRow" select="$dvt_RowCount" />
</xsl:call-template>

Export Web Part
Select Web Part in the designer and go to "Web Part" tab, select "To File" option.

Include Web Part in a Visual Studio solution
  • Go to SharePoint project in the Visual Studio and add a Web Part control (Ex: WebPart1).
  • Delete “WebPart1.cs” file since we are not going to use it.
  • Open “WebPart1.webpart” file and replace its content by the content in exported file.
  • Remove "ListId", "ListName" properties in the “WebPart1.webpart”.