SharePoint Check User Permission

This is code snippet to check if user has role permission

/// Find the particular role definition to check agaionst the user has permisson
SPRoleDefinition roleDefinition = web.RoleDefinitions.GetByType(SPRoleType.Administrator);
SPUser siteUser = web.SiteUsers[“SP\\mukesh”];

///Get all permission for the selected user
SPPermissionInfo userPermissions = web.GetUserEffectivePermissionInfo(siteUser.LoginName);

///Check role is is assigned to the user
var memberof = userPermissions.RoleAssignments.Where(role => role.RoleDefinitionBindings.Contains(roleDefinition)).Select(roleMember => roleMember.Member).ToList();

 

 

Advertisements

How To avoid assembly registration in Web.Config for the Object Data Source

It’s very common to use object data source to bind the grid , formview etc but the problem is Assembly registration in web.config specially in multi server farm.
In order to avoid the registration of your assembly in web.config file just follow two simple steps as mentioned below.

Step-1

In your UserControl ASCX file add assembly directive

<%@ Assembly Name=”ILX.MKS.Business, Version=1.0.0.0, Culture=neutral, PublicKeyToken=123a45b9c456789″ %>

    Step-2

    In object data source give fully qualified type Name like below

    TypeName=”ILX.MKS.Business.UserRegistration, ILX.MKS.Business, Version=1.0.0.0, Culture=neutral, PublicKeyToken=123a45b9c456789″

 
Note: –
There are pros and cons of the above option, this is helpful if you do not want to touch the web.config file, otherside is if you maintain in web.config then  will be central location to maintain
the assembly reference.

 

 

 

JQuery Add new item in table

Code sample

—-

Click here to View Demo 

—-

<!DOCTYPE html>
<html>
<head>
<script src=”http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js”></script&gt;
<style>
table, td {
border: 0px solid black;
}
</style>
</head>
<body>

<p>Click the button to add a new row at the first position of the table and then add cells and content.</p>
<table style=’display:none;border: 1px solid red;’ id=”myTable”>
<tr>
<td><input type=’text’/></td>
<td>
<select>
<option value=”volvo”>Volvo</option>
<option value=”saab”>Saab</option>
<option selected value=”mercedes”>Mercedes</option>
<option value=”audi”>Audi</option>
</select>
</td>
<td><input type=”button” value=”Delete Row”></td>
</tr>

</table>
<div class=”dynamiTbl”>
<table id=”myTableMain”>
<tr>
<th>Month</th>
<th>Savings</th>
<th>Savings</th>
</tr>
<tr>
<td><input type=’text’/></td>
<td>
<select>
<option value=”volvo”>Volvo</option>
<option value=”saab”>Saab</option>
<option selected value=”mercedes”>Mercedes</option>
<option value=”audi”>Audi</option>
</select>
</td>
<td><input type=”button” value=”Delete Row”></td>
</tr>

</table>
</div>
<br>

<button id=’btnAddMore’ onclick=”myFunction()”>Add more</button>

<script>
var totalItem=0;
function myFunction() {
var table = document.getElementById(“myTable”);

var tableMain = document.getElementById(“myTableMain”);

var clone = table.getElementsByTagName(‘tr’)[0].cloneNode(true); // copy children too
if(tableMain.getElementsByTagName(‘tr’).length <= 10)
{
var tds =clone.cells[0];//
var inptText = tds.getElementsByTagName(‘input’);
inptText[0].value=”;
tds =clone.cells[1];//
var inptSelect = tds.getElementsByTagName(‘select’);
inptSelect[0].selectedIndex =-1;

tableMain.appendChild(clone);
}
else
{
$(‘#btnAddMore’).hide();
}

}
$(function(){
//alert(1);
//$(‘input[type=”button”]’).click(function(e){
//$(this).closest(‘tr’).remove();});

$(“.dynamiTbl”).on(“click”, “#myTableMain tr input[type=’button’]”, function(e) {
$(this).closest(‘tr’).remove();
$(‘#btnAddMore’).show();
});

});

</script>

</body>
</html>

How to create List with Item Order enabled

In order to enable feature in a list to change item order, it will be better
to create your list definition go to schema.xml and specify OrderedList=”TRUE”

and in the same schema.xml file go to view and change Order by CAML like..

<View BaseViewID=”1″ OrderedView=”TRUE”
…………………..
…………………..

<Query>
<OrderBy>
<FieldRef Name=”Order”></FieldRef>
</OrderBy>
</Query>

………..

Now add some data into the list created based on above list definition
and then go to http://yoursitename/_layouts/Reorder.aspx?List=listGuid

This is manual process but can be added into ribbon button by adding
custom action for the list definition.

 

 

Display User field in search result

How to display custom user field in search result .

This includes two steps field definition and create a managed metadata property.

1. Field Definition

<Field ID=”{AE6EFA2E-DF0E-4EF5-8AB7-309FBC5465B9}”

Group=”My Columns”
Mult=”FALSE”
UserSelectionMode=”0″
Type=”UserMulti” List=”UserInfo”
Name=”helloauthor” DisplayName=”helloauthor”
SourceID=”http://schemas.microsoft.com/sharepoint/v3&#8243;
StaticName=”helloauthor”

/>

2. Before creating managed property add some data to the field may be in your
documents library or custom list etc… and then crawl the search , once
your property is propagated  then create managed property.

Now re crawl your search and the perform search based on user name.

Upload file in SharePoint Style Library using Powershell

This Powershell code snippet upload xsl file into style library.

 

 

if ( (Get-PSSnapin -Name Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue) -eq $null )
{
Add-PsSnapin Microsoft.SharePoint.PowerShell
}

#Script settings
$webUrl = “http://MKS&#8221;

function UploadFile( [String] $FilePath)
{
#Open web and library
$web = Get-SPWeb $webUrl

$file =Get-Item $FilePath
write-host “Started Uploading File……”$file.FullName

#Open file
$fileStream = ([System.IO.FileInfo] (Get-Item $file.FullName)).OpenRead()

$folder =  $web.getfolder(“Style Library”)

$web.GetFile($folder.Url + “/XSL Style Sheets/” + $file.Name).CheckOut()

$spFile = $folder.Files.Add($folder.Url + “/XSL Style Sheets/” + $file.Name, [System.IO.Stream]$fileStream, $true)

$spFile.CheckIn(“Checkin by Powershell”)
$spFile.Publish(“Published by Powershell”)

write-host $webUrl”/”$folder.Url”/XSL Style Sheets/”$file.Name

write-host “Successfully Copyied file ” $file.Name

$fileStream.Close();

$web.Dispose()
}

UploadFile -FilePath “C:\***.xsl”

SharePoint 2010 FAST 2010 Unexpected error occured while communicating with Administration

One of the annoying error in FAST 2010 when you clicks on the manage property
it blows up and throw error..

Unexpected error occurred while communicating with Administration … ThreadAbortException : Thread was being aborted. at System …

To avoid this type of error just need to make the deployment and configuration

clean from the beginning.

1. Create a domain user give that user sufficient Administrative privileged.

2. Go to SQL Server give permission to create database permission or
add it in serveradmin role.

3. Login with newly created user and run the FAST setup.

4. After that run the FAST Configuration wizard.

5. Go to Active directory and verify “FASTSearchAdministrators” group and
the members inside.

6.Install the certificates and follow the instruction.
http://technet.microsoft.com/en-us/library/ff381253.aspx

7. run the command [nctrl status] verify if services are running
if not running then start it using powershell command or from services.

8. Go to central admin create Search Query and Connector and give permission.