Sunday 16 May 2010

Set access level of custom webpart

There are two places where we can deploy custom webpart assembley in order to use it in SharePoint.


a. deploy it to Global Assembley Cache
b. copy it to bin directory of relevant web application.


and there are 2 ways to set trust level


a. change default trust level from WSS_Minimal to WSS_Midium or WSS_Full manually in web.config
b. create custom access policy and deploy it to web.config.


Now, here is the code you can use to deploy your custom access policy in web.config

<CodeAccessSecurity>


<PolicyItem>

<PermissionSet class="NamedPermissionSet" version="1" Description="Permission set for custom test WebParts">

<IPermission class="AspNetHostingPermission" version="1" Level="Minimal" />

<IPermission class="SecurityPermission" version="1" Flags="Execution" />

<IPermission class="Microsoft.SharePoint.Security.SharePointPermission, Microsoft.SharePoint.Security, version=11.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" version="1" ObjectModel="True" />

<IPermission class="System.Net.WebPermission, System, version=1.0.5000.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1">

<ConnectAccess>

<URI uri="https?://.*" />

</ConnectAccess>

</IPermission>

<IPermission class="System.Security.Permissions.SecurityPermission, mscorlib, version=1.0.5000.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Flags="ControlThread, UnmanagedCode" />

<IPermission class="System.Security.Permissions.EnvironmentPermission, mscorlib, version=1.0.5000.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Read="UserName" />

</PermissionSet>

<Assemblies>

<Assembly Name="HelloWorld" />

</Assemblies>

</PolicyItem>

</CodeAccessSecurity>
 
You can only have one CAS policy per solution package but can have mulitple asseblies in it. It is always good practice to use CAS policy  instead of doing manual changes as it applies to all the assemblies in the bin directory of web application.

Wednesday 14 April 2010

Convert PersonOrGroup field to SPUser

There is no straight way to convert "personorgroup" field to SPUser object. Here the way to convert it using SPFieldUser object.



private
SPUser GetUser(SPListItem item, SPField userField)
{
string currentValue = item[userField.Title].ToString();
SPFieldUser field = (SPFieldUser)userField;
SPFieldUserValue fieldValue = (SPFieldUserValue)field.GetFieldValue(currentValue);
return fieldValue.User;
}

Tuesday 13 April 2010

Create Open Tool Pane link in custom sharepoint webpart.

Below is the way to open a tool pane from custom webpart.


<a href=\"javascript:MSOTlPn_ShowToolPane2Wrapper('Edit', this,'" + this.ID + "')\">Open tool pane</a>


Just add this link in the webpart and it will do the trick. It even works fine in SharePoint 2010!!!




Happy Coding...



Use [Me] in CAML query when user is part of group

[Me] keyword does not work when user is part of a sharepoint group and that group is assigned to any item or list. Here is the way to make it work
<Where>
                <Or>
                                <Membership Type="CurrentUserGroups">
                                                <FieldRef Name="AssignedTo"/>
                                </Membership>
                <Eq>
                                <FieldRef Name='AssignedTo'/>
                                <Value Type='Integer'>
                                                <UserID Type='Integer'/>
                                </Value>
                </Eq>
                </Or>
</Where>