Thursday, August 14, 2014

Snippets to create custom SharePoint forms/pages

Many times I create custom SharePoint forms in my solutions. More appropriate way is to create SharePoint form using InputFormSection. InputFormSection takes care of the layout and positioning of your controls. You can add InputFormControl containing asp.net/SharePoint controls under InputFormSection.

Add the following references in .aspx pages:

<%@ Register TagPrefix="SharePoint"
Namespace="Microsoft.SharePoint.WebControls"
Assembly="Microsoft.SharePoint, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register TagPrefix="wssuc" TagName="InputFormSection" Src="~/_controltemplates/InputFormSection.ascx" %>
<%@ Register TagPrefix="wssuc" TagName="InputFormControl" Src="~/_controltemplates/InputFormControl.ascx" %>
<%@ Register TagPrefix="wssuc" TagName="ButtonSection" Src="~/_controltemplates/ButtonSection.ascx" %>
view raw References.aspx hosted with ❤ by GitHub

Container for InputFormSection:

<table border="0" cellspacing="0" cellpadding="0" width="100%">
<!--Add InputFormSection-->
</table>

Followings are some snippets of few common controls I use in custom SharePoint forms/pages:

Texbox


<wssuc:InputFormSection Title="Input Title" Description="Input Description" runat="server">
<template_inputformcontrols>
<wssuc:InputFormControl LabelText="" LabelAssociatedControlId="inputID" runat="server">
<Template_Control>
<SharePoint:InputFormTextBox title="Name" class="ms-input" ID="inputID" Columns="35" runat="server" MaxLength="256" />
<SharePoint:InputFormRequiredFieldValidator ID="InputValidator" ControlToValidate="inputID" ErrorMessage="Required field" Width="300px" runat="server" />
</Template_Control>
</wssuc:InputFormControl>
</template_inputformcontrols>
</wssuc:InputFormSection>






Multiline Textbox


<wssuc:InputFormSection runat="server" Title="MultiLine Textbox Title" Description="MultiLine Textbox Description">
<template_inputformcontrols>
<wssuc:InputFormControl runat="server" LabelText="">
<Template_Control>
<sharepoint:InputFormTextBox title="Title" class="ms-input" ID="TxtCreateSiteDescription" Runat="server" TextMode="MultiLine" Columns="40" Rows="3"/>
</Template_Control>
</wssuc:InputFormControl>
</template_inputformcontrols>
</wssuc:InputFormSection>






Dropdown


<wssuc:InputFormSection Title="Dropdown Title" Description="Dropdown Description" runat="server">
<template_inputformcontrols>
<wssuc:InputFormControl LabelText="" LabelAssociatedControlId="dropdownID" runat="server">
<Template_Control>
<asp:DropDownList ID="dropDownID" runat="server" width="300">
<asp:ListItem Text="Select" Value="0" Selected="True" />
</asp:DropDownList>
<SharePoint:InputFormRequiredFieldValidator ID="DropDownValidator" ControlToValidate="dropDownID" InitialValue="0" ErrorMessage="Required field" Width="300px" runat="server" />
</Template_Control>
</wssuc:InputFormControl>
</template_inputformcontrols>
</wssuc:InputFormSection>



Checkbox


<wssuc:InputFormSection Title="Checkbox Title" Description="Checkbox Description" runat="server">
<Template_InputFormControls>
<wssuc:InputFormControl LabelText="" LabelAssociatedControlId="checkBoxID" runat="server">
<Template_Control>
<asp:CheckBox ID="checkBoxID" runat="server" Text="Some description about checkox" Title="Checkbox Title" Checked="true" />
</Template_Control>
</wssuc:InputFormControl>
</Template_InputFormControls>
</wssuc:InputFormSection>





Radio button


<wssuc:InputFormSection Title="Radio Button Title" Description="Radio Button Description" runat="server">
<Template_InputFormControls>
<wssuc:InputFormControl LabelText="" LabelAssociatedControlId="" runat="server">
<Template_Control>
<asp:RadioButton ID="radioButtonID1" runat="server" Text="Choice 1" GroupName="RadioButtonGroup" Title="Radio Button 1 Title" />
<asp:RadioButton ID="radioButtonID2" runat="server" Text="Choice 2" GroupName="RadioButtonGroup" Title="Radio Button 2 Title" />
</Template_Control>
</wssuc:InputFormControl>
</Template_InputFormControls>
</wssuc:InputFormSection>






Url Field


<wssuc:InputFormSection Title="Url Title" Description="Url Description" runat="server">
<template_inputformcontrols>
<wssuc:InputFormControl runat="server">
<Template_Control>
<table border="0" cellspacing="1" class="ms-authoringcontrols" width="100%">
<tr>
<td class="ms-formdescription" nowrap="nowrap" width="100%">
<SharePoint:EncodedLiteral runat="server" text="Type the Web address: " />
<a href="javascript:" onclick="javascript:TestURL('<%= urlInputID.ClientID %>');return false;">
<SharePoint:EncodedLiteral runat="server" text="(Click here to test)" EncodeMethod='HtmlEncode'/>
</a>:
</td>
</tr>
<tr>
<td class="ms-authoringcontrols" width="100%" >
<SharePoint:InputFormTextBox title="Url Title" CssClass="ms-input" ID="urlInputID" Columns="35" Runat="server" text="http://" maxlength="255" width="300" />
</td>
</tr>
<tr>
<td class="ms-formdescription nowrap="nowrap" >
<SharePoint:EncodedLiteral runat="server" text="Type the description:" EncodeMethod='HtmlEncode'/>
</td>
</tr>
<tr>
<td nowrap="nowrap" class="ms-authoringcontrols" width="100%">
<SharePoint:InputFormTextBox title="Url Description Title" CssClass="ms-input" ID="InputFormTextBox2" Columns="35" Runat="server" maxlength="255" width="300" />
<SharePoint:InputFormRequiredFieldValidator ID="urlRequiredValidator" InitialValue="http://" ControlToValidate="urlInputID" ErrorMessage="Required field" runat="server" Display="Dynamic" CssClass="ms-error"/>
<SharePoint:InputFormRegularExpressionValidator ID="urlRegularExpressionValidator" ControlToValidate="urlInputID" ErrorMessage="URL is invalid." runat="server" ValidationExpression="http(s)?://([\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)?" Display="Dynamic" CssClass="ms-error"/>
</td>
</tr>
</table>
</Template_Control>
</wssuc:InputFormControl>
</template_inputformcontrols>
</wssuc:InputFormSection>









File Upload


<wssuc:InputFormSection runat="server" title="File Upload Title" Description="File Upload Description">
<Template_InputFormControls>
<wssuc:InputFormControl runat="server" SmallIndent="true">
<Template_Control>
<table class="ms-authoringcontrols" width="100%">
<tr><td>
<input type="file" id="InputFile" runat="server" class="ms-fileinput ms-fullWidth" size="35" />
</td></tr>
<tr><td>
<SharePoint:InputFormRequiredFieldValidator ControlToValidate="InputFile"
Display="Dynamic" ErrorMessage="Select the document" Runat="server"/>
</td></tr>
<tr><td><asp:CheckBox id="OverwriteSingle" Checked="true" Text="Overwrite existing files" runat="server" CssClass="ms-upload-overwrite-cb" /></td></tr></table>
</Template_Control>
</wssuc:InputFormControl>
</Template_InputFormControls>
</wssuc:InputFormSection>







People Picker


<wssuc:InputFormSection Title="User Title" Description="User Description" runat="server">
<template_inputformcontrols>
<wssuc:InputFormControl LabelText="User name:"
runat="server">
<Template_Control>
<Sharepoint:PeopleEditor AllowEmpty=false SingleLine=true ValidatorEnabled="true" MultiSelect=false id="PickerOwner" runat="server" SelectionSet="User" />
</Template_Control>
</wssuc:InputFormControl>
</template_inputformcontrols>
</wssuc:InputFormSection>





Button


<wssuc:ButtonSection runat="server">
<template_buttons>
<asp:Button
runat="server"
OnClick="Onclick_Method"
class="ms-ButtonHeightWidth"
Text="<%$Resources:wss,multipages_okbutton_text%>"
id="_btnSubmit"
accesskey="<%$Resources:wss,okbutton_accesskey%>"
Enabled="true"/>
</template_buttons>
</wssuc:ButtonSection>


Tuesday, August 12, 2014

Sharepoint Selector Controls for custom administration pages

Sharepoint comes with four selector controls to choose destination elements for the operations.  These controls can be used in custom administration pages.  You can locate this controls under Microsoft.SharePoint.WebControls namespace.  To add selector controls you have include following page directive:

<%@ Register TagPrefix="SharePoint"
Namespace="Microsoft.SharePoint.WebControls"
Assembly="Microsoft.SharePoint, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>


WebApplicationSelector

WebApplicationSelector control allow user to get or set the web application.

<SharePoint:WebApplicationSelector id="webAppSelector" runat="server" />



SiteAdministrationSelector

SiteAdministrationSelector control allow user to get or set the site collection.

<SharePoint:SiteAdministrationSelector id="siteSelector" runat="server" />




WebAdministrationSelector

WebAdministartionSelector allow user to gets or sets the Web.  The web selector requires that SiteSelector not be a null reference.  So you have to use SiteAdministartionSelector to use this control.
<SharePoint:SiteAdministrationSelector id="siteSelector" runat="server" />
<SharePoint:WebAdministrationSelector id="webSelector" runat="server" />
protected void Page_Load(object sender, EventArgs e)
{
// Set SiteSelector for Web
webSelector.SiteSelector = siteSelector;
}


ListAdministrationSelector

ListAdministrationSelector allow user to get or set the List or Document Library.  The List selector requires WebAdministartionSelector.  So if you want to use list selector then you have to use WebAdministartionSelector as well as SiteAdministrationSelector.

<SharePoint:SiteAdministrationSelector id="siteSelector" runat="server" />
<SharePoint:WebAdministrationSelector id="webSelector" runat="server" />
<SharePoint:ListAdministrationSelector id="listSelector" runat="server" />
protected void Page_Load(object sender, EventArgs e)
{
webSelector.SiteSelector = siteSelector;
listSelector.SiteSelector = siteSelector;
listSelector.WebSelector = webSelector;
}




To get the selected values from above controls use CurrentItem property:

SPWebApplication webApp = webAppSelector.CurrentItem;
SPSiteAdministration site = siteSelector.CurrentItem;
SPWeb web = webSelector.CurrentItem;
SPList list = listSelector.CurrentItem;
view raw Selectors.cs hosted with ❤ by GitHub

Friday, August 1, 2014

Get operating system name from HttpRequest

I was creating a HttpModule in SharePoint to track the request related data such as browser name and client platform. If you are tracking request related data then you must be familiar with System.Web.HttpBrowserCapabilities class. HttpBrowserCapabilities class enables the server to gather information on the capabilities of the browser that is running on the client. I was using HttpCapabilitiesBase.Platform Property to get the client operating system name. But for Windows platform it was returning "WinNt" instead of actual operating system name. After doing some research I noticed that Request.UserAgent returns operating system information along with operating system version number. 

Here is my code to get the Operating System name from Request.UserAgent

/// <summary>
/// Get OS Name from User Agent
/// </summary>
/// <returns>OS Name</returns>
private string GetOsName()
{
// Create collection of user agent string and OS Name
var list = new List<KeyValuePair<string, string>>();
list.Add(new KeyValuePair<string, string>("windows nt 6.3", "Windows 8.1"));
list.Add(new KeyValuePair<string, string>("windows nt 6.2", "Windows 8"));
list.Add(new KeyValuePair<string, string>("windows nt 6.1", "Windows 7"));
list.Add(new KeyValuePair<string, string>("windows nt 6.0", "Windows Vista"));
list.Add(new KeyValuePair<string, string>("windows nt 5.2", "Windows Server 2003/XP x64"));
list.Add(new KeyValuePair<string, string>("windows nt 5.1", "Windows XP"));
list.Add(new KeyValuePair<string, string>("windows xp", "Windows XP"));
list.Add(new KeyValuePair<string, string>("windows nt 5.0", "Windows 2000"));
list.Add(new KeyValuePair<string, string>("windows me", "Windows ME"));
list.Add(new KeyValuePair<string, string>("win98", "Windows 98"));
list.Add(new KeyValuePair<string, string>("win95", "Windows 95"));
list.Add(new KeyValuePair<string, string>("win16", "Windows 3.11"));
list.Add(new KeyValuePair<string, string>("macintosh|mac os x", "Mac OS X"));
list.Add(new KeyValuePair<string, string>("mac_powerpc", "Mac OS 9"));
list.Add(new KeyValuePair<string, string>("linux", "Linux"));
list.Add(new KeyValuePair<string, string>("ubuntu", "Ubuntu"));
list.Add(new KeyValuePair<string, string>("iphone", "iPhone"));
list.Add(new KeyValuePair<string, string>("ipod", "iPod"));
list.Add(new KeyValuePair<string, string>("ipad", "iPad"));
list.Add(new KeyValuePair<string, string>("android", "Android"));
list.Add(new KeyValuePair<string, string>("blackberry", "BlackBerry"));
list.Add(new KeyValuePair<string, string>("webos", "Mobile"));
string userAgent = application.Request.UserAgent;
string os = string.Empty;
for (int i = 0; i < list.Count; i++)
{
if (userAgent.ToLower().IndexOf(list[i].Key.ToLower())>0)
{
// Set OS Name for particuler user agent
os = list[i].Value;
break;
}
}
return os;
}
view raw GetOSName.cs hosted with ❤ by GitHub
If you find more combinations please add it to comments section.