Tuesday, 26 February 2013

Populating current logged in user in people picker

Sometimes there are requirement to populate the current logged in user in People picker field.
I had tried to populate using PickerEntity class, but it use to fail on click of "Check Names" button. So changed the approch to se the comma seprated names and it worked.

1. Take the curent Logged user

loginName = SPContext.Current.Web.CurrentUser.LoginName

2. Assign the login to picker control. 
pkrControlName.CommaSeparatedAccounts =loginName

Note: You can have multiple comma serpated login names to populate multiple users in people picker field.

Wednesday, 5 September 2012

Retrieve existing base permission using powershell

Below script can be used to retrieve the existing Base Permission for the given permission level.
param(

[Parameter(Position=0, Mandatory=$true)]
[string]
$siteURL
)
$snapin = Get-PSSnapin
Where-Object { $_.Name -eq "Microsoft.SharePoint.Powershell" }
if ($snapin -eq $null) {
Add-PSSnapin "Microsoft.SharePoint.Powershell"
}

$site=Get-SPSite $siteURL
$web=$site.RootWeb
$permissionLevel=$web.RoleDefinitions["PermissionName"]
Write-Host $permissionLevel.BasePermissions
$web.Dispose()
$site.Dispose()

Thursday, 24 November 2011

Save ViewState at server side to reduce Page Size

Some time we have lots of heavy controls (like GridView,DataGrid) which emits lots of ViewState information on the page. This results to increase in Page size and apparently it would reduce the page performance.
So to reduce the page size ViewState can be stored on Server side which would reduce the page size.
We need to override SavePageStateToPersistenceMedium method to save the ViewState at server side and LoadPageStateFromPersistenceMedium method to retrieve the ViewState.

Note: These two methods needs to be overridden in code behind file of ASP.Net page.

Sample Code snippet.

protected override void SavePageStateToPersistenceMedium(object state)
{
try
{
string VSKey = "VIEWSTATE_" + base.Session.SessionID + "_" +
Request.RawUrl + "_" + DateTime.Now.Ticks.ToString();
Cache.Add(VSKey, state, null, DateTime.Now.AddMinutes(Session.Timeout),
Cache.NoSlidingExpiration, CacheItemPriority.Default, null);
ClientScript.RegisterHiddenField("__VIEWSTATE_KEY", VSKey);
}
catch
{
base.SavePageStateToPersistenceMedium(state);
}
}

protected override object LoadPageStateFromPersistenceMedium()
{
string VSKey = Request.Form["__VIEWSTATE_KEY"];
return Cache[VSKey];
}

Wednesday, 3 August 2011

Admin SVC must be running in order to create deployment timer job

Issue : Recently, while using a PowerShell command (Update-SPSolution) I encountered below error.

Admin SVC must be running in order to create deployment timer job

Solution : You would encounter above error if “ SharePoint 2010 Administration” services is nor running. So to resolve above issue goto run -> type “services.msc” and start “SharePoint 2010 Administration” service.

Friday, 1 July 2011

Multiple list instances of list definition

Problem:

Recently, I observed one weird thing in my SharePoint application.I was creating a list definition which had multiple views. I wanted to create one instance of the site definition but I could see multiple list instances of the same list in my site.

Reason:

I figured out the reason behind this issue.

In schema.xml under the views node I had multiple views definitions.
I had created one view and replicated the same for rest of the views.

<View DisplayName="By Customer Name" DefaultView="TRUE" BaseViewID="1" Type="HTML" MobileView="TRUE" MobileDefaultView="TRUE" ImageUrl="/_layouts/images/generic.png" XslLink="main.xsl" WebPartZoneID="Main" Url="By Customer Name.aspx" SetupPath="pages\viewpage.aspx">
<Toolbar Type="Standard" />
<XslLink>main.xsl</XslLink>
<Query>
</Query>
<ViewFields>
</ViewFields>
<RowLimit Paged="TRUE">100</RowLimit>
</View>


I was modifying the query and fieldrefs. Erroneously DefaultView="TRUE" attribute was present in all the views, which was resposible for creating multiple instances of the list and keeping that particular view as the default view.

Sunday, 19 June 2011

How to programmatically download attachments from list items

To download an attachment, you must first find the download link and then redirect to another page.

The ending of the response from current page does not affect the functionalities on the second one.


using (SPSite site = new SPSite("http://SiteCollectionURL"))
{
using (SPWeb web = site.OpenWeb())
{
string file = string.Empty;
SPList list = web.Lists["MyList"];
SPListItem currentItem = myList.GetItemById(id);
if (currentItem["AttachmentName"] != null)
{
file = "/Lists/ MyList/Attachments/" +
id.ToString() + "/" +
currentItem["AttachmentName"].ToString();
System.Web.HttpContext.Current.Session["FileName"] =
currentItem["AttachmentName"].ToString();
System.Web.HttpContext.Current.Session["Attachment"] =
file.Trim();
}
else
{
lblReport.Text = "No File name found";
}
if (file != string.Empty)
{
Reponse.Redirect("download.aspx");
}
}
}

On the download.aspx page, you need to the code shown below to download the file.

if (System.Web.HttpContext.Current.Session["Attachment"] != null)
{
string strName = System.Web.HttpContext.Current.Session["FileName"].ToString();
string sbURL = System.Web.HttpContext.Current.Session["Attachment"].ToString();
System.Web.HttpResponse response;
response = System.Web.HttpContext.Current.Response;
System.Web.HttpContext.Current.Response.ContentEncoding =
System.Text.Encoding.Default;
response.AppendHeader("Content-disposition", "attachment; filename=" + strName);
response.AppendHeader("Pragma", "cache");
response.AppendHeader("Cache-control", "private");
response.Redirect(sbURL);
response.End();
}

Deleting an Attachment from SPList

Use below method to delete attachment of specific list item.

public void DeleteAttachment(int id,string fileName)
{
using (SPSite site = new SPSite("http://SiteCollectionURL"))
{
using (SPWeb web = site.OpenWeb())
{
SPList list = web.Lists["MyListName"];
SPListItem delItem = list.GetItemById(id);
SPAttachmentCollection files = delItem.Attachments;
files.Delete(fileName);
delItem.Update();
}
}
}

Query List/Document Library in Specific Folder

To query SharePoint List or Document Library in specific Folder “ FolderServerRelativeUrl ” as part of the CAML Query Code Snippet ...