Wednesday, 26 January 2011

Add/remove sharepoint group using power shell

#Add a sharepoint group using power shell

#Create a sharepoint custom permission using Power shell

$web = get-SPWeb "http://sitecollectionName"
$member = $web.AllUsers["domain\username"]
$CustomPermission ="Permission Name"
$CustomRoleRoledef = New-Object "Microsoft.SharePoint.SPRoleDefinition"
$CustomRoleRoledef.Name = "$CustomPermission"
$CustomRoleRoledef.Description = "Custom permission level for Site Owners"
$CustomRoleRoledef.BasePermissions =
"CancelCheckout,AddListItems,EditListItems,DeleteListItems,ViewListItems,OpenItems,ViewVersions,DeleteVersions,CreateAlerts,ViewFormPages,ViewUsageData,ViewPages,BrowseUserInfo,ManageAlerts,UseRemoteAPIs,UseClientIntegration,Open"
$web.RoleDefinitions.Add($CustomRoleRoledef)
$web.Update()

# Create custom group
$CustomGroup ="My Custom Group"
$web.SiteGroups.Add("$CustomGroup", $member, $null,"Custom Group ")

#Assign Permission to the Group
$customPermission = $web.RoleDefinitions["$CustomRoleRoledef "]
# If you don't want to use custom permission, you can directly give existing permission name i.e "Full Control/Contributor/Design/Visitor"
#$existingPermission = $web.RoleDefinitions["Design"]
$customPermissionAssignment = New-Object "Microsoft.SharePoint.SPRoleAssignment" -ArgumentList $web.SiteGroups["$CustomGroup"]
$customPermissionAssignment.RoleDefinitionBindings.Add($customPermission)
$web.RoleAssignments.Add($techAdminRoleAssignment)


#Remove a sharepoint group using power shell

#Remove Groups if already exist
$GroupName ="Custom Group"
$objSiteGroup = $web.SiteGroups["$GroupName"]
if ($objSiteGroup)
{
$web.SiteGroups.Remove("$GroupName")
}


Note: you can save above script in .ps1 extension file and execute in power shell

Monday, 24 January 2011

The Server Returned a non-specific error when trying to get data from the data source

This issue occurs when you do backup restore of the site/site collection. Due to this you can not see the page in design mode due to some reference changes and will not be able to add any new data sources on the page.

To resolve this issue follow below steps.

1. Go to Insert - New Item Form
2. Click on Custom List Form
3. Choose your custom list.
4. Choose form type (new/edit/display).
5. Select the "DataSources" section of the newly created dataformwebpart.
6. Now replace the existing "Datasources" section with selected one.
7. Remove the old DataFormweb part.


Just after saving the form mentioned error should go and you should be able to see all the controls with existing binding.

Thursday, 23 December 2010

Manipulating web.config file programmatically

To modify/add a web config entry following code can be referred

// Get the SPSite object
SPSite site = new SPSite("URL");
if (site != null)
{
SPWebApplication webApp = site.WebApplication;
// Create the modification entry
SPWebConfigModification webEntry = new SPWebConfigModification("Web Config entry " ,"Entry location" );
// Add the web config entry to the collection
webApp.WebConfigModifications.Add(webEntry);
webApp.Farm.Services.GetValue().ApplyWebConfigModifications();
}

Web Config Entry could be

"SafeControl[@Assembly="MyAssembly"][@Namespace="My.Namespace"]"
+ @"[@TypeName="*"][@Safe="True"][@AllowRemoteDesigner="True"]"


Entry location could be

configuration/SharePoint/SafeControls

Wednesday, 15 December 2010

Could not load file or assembly CustomMarshalers

Recently, I came across a weird issue while running the code in different environment.

Suddenly I started getting "Could not load file or assembly CustomMarshalers" at certain line of my code. It was difficult to understand why same code is not running in new environment. I investigated a bit and found that "CustomMarshalers" is required for compact framework which is not at all useful in my application. Ii couldn't found any solution except changing my code.

1. I had used LINQ queries at few places in my application so i had to change it to CAML query to run in all the environment

2. Wherever I was using "foreach" block and using Sharepoint object collection like SPListItemCollection, I was getting mentioned error. So I had replace all the "foreach" blocks with "for" construct.

I hope these information may help somebody....

Backup and restore using powershell command

Backup

backup-spsite -identity "site-collection-path" -path "backup-location"

where

site-collection-path : http://machinename:port/sites/sitecollectionname

backup-location : D:/backup/site.bak


Restore

restore-spsite -identity "site-collection-path" -path "backup-location" -Force

Note: You can not restore more than one site collection in a web application so you have to create a new database. Below command can be used to overcome this issue.

restore-spsite -identity "site-collection-path" -path "backup-location" -Force -DatabaseName "DBNAME"


To Create a new Database follow below instruction

Goto Central Administration---> Manage Content Databases-->Add New Content Database

Monday, 13 September 2010

This view cannot be displayed because the number of lookup and workflow status columns it contains exceeds the threshold (8) enforced by the admin..

Issue:

This view cannot be displayed because the number of lookup and workflow status columns it contains exceeds the threshold (8) enforced by the administrator

Solution

Sharepoint 2010 has default "Resource Throttling" settings. Due to which this warning would be encountered. To overcome this warning, we can increase the number of lookup columns in following ways.


* Goto --> Application Management --> Manage Web Application.
* Select your web application.
* General Settings (on Ribbon window) --> Resource Throttling.
* Resource Throttling window, goto "List View Lookup Threshold" and give the value as per your requirement.

Tuesday, 31 August 2010

Alternative of using _spBodyOnLoadFunctionNames

I am very sure that most of the Sharepoint developers must have used _spBodyOnLoadFunctionNames to inject javascript methods on the sharepoint list forms(NewForm/EditForm/DispForm).

Downside of using _spBodyOnLoadFunctionNames is the time taken to execute my javascript function. Even though my function is very small, there is delay in executing my method.

Reason for the Delay:

Delay occurs because sharepoint first loads all the controls on the form and subsequently executes the embedded javascript functions. So if number of controls are high, it will take substantial time to execute my custom javascript method. Delay would be more in case of Sharepoint 2010 pages because it has more number of inbuilt javascript functions to execute while loading the page.

Solution:

In order to avoid the delay we have to execute our function "onload" event of any control and following steps can be considered to achieve this functionality.

1. Create your javascript function
function MyMethod()
{
// My method logic
}

2.Create a "img" element anywhere on your page(Since we can not capture "onload event" of most of the controls).
3. Set the image source to "/_layouts/images/blank.gif" so that nothing is visible on the page.

4. Call your javascript function on "onload event of the img.


<IMG SRC="/_layouts/images/blank.gif" width="1" height="18" onload="MyMethod();"/>

Note: Mentioned issue is quite visible in case you are trying to hide/show some buttons/controls of the page on load of the page.

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 ...