Tuesday, March 27, 2012

Add List Items with users identity (Created By) when using RunWithElevatedPriviledges delegate

We faced a situation when we have to elevate the read only users permission to add/update the list items in SharePoint list. To accomplish this we usually go for the RunWithElevatedPriviledges delegate, but the only disadvantage of this approach is if we add/update any item within this delegate, it will save that record with 'System Account' identity.

Following code can be used to overwrite the 'System Account' identity with current user's identity, So list item will display 'Created By' or 'Modified By' columns value as current user.

SPUser user = SPContext.Current.Web.CurrentUser;

SPSecurity.RunWithElevatedPrivileges(delegate()
{
    using (SPSite site = new SPSite(SPContext.Current.Web.Url))
    {
        using (SPWeb web = site.OpenWeb())
        {
            web.AllowUnsafeUpdates = true;
            SPList list = web.Lists["TempList"];
            SPListItem listItem = list.AddItem();
            listItem["Title"] = string.Format("Hey {0}", user.LoginName);
            listItem["Editor"] = user;  // Modified By                    
            listItem["Author"] = user;  // Created By
            listItem.Update();
            web.AllowUnsafeUpdates = false;
        }
    }
});
 

Friday, March 9, 2012

Simplest way to get the strong name of an assembly

I found very interesting post which describes how to get strong name of the assembly without even opening the properties window of that assembly. I saw many fellow-developers are still not aware of this simple option that may make their life at least a bit easier, hence sharing this post.

Old Method:

Open the assembly in the GAC (c:\Windows\Assembly) and view its properties. Then form the strong name with assembly name, version and public key token.


Simplest method:

Open the assembly, select the assembly you need the strong name for, then from the Edit menu choose Copy Display Name menu item


Tuesday, March 6, 2012

Session State Service Configuration in SharePoint 2010

To enable ASP.NET session state, log on the Central Admin
Server using Farm Admin Account, and Either Run PowerShell command 
Enable-SPSessionStateService -DefaultProvision

to create service application with default
state. By default, this will create service application database with
“SessionStateService_<GUID>”, on the same database server where farm
configuration database is located using windows credentials of the logged in
user.

Or
 
Enable-SPSessionStateService -DatabaseServer YourDBServerName
-DatabaseName YourDBName

to create service application
with specific database name on non-SharePoint configuration database server.
For more details and additional parameters,
Reference link: http://technet.microsoft.com/en-us/library/ff607857.aspx




By enabling Session State Service on your farm,
It would create database on specified server:  SessionStateService_<GUID>


It would create SharePoint Server Session State Service in
Manage Service Applications, please make sure it is showing ‘Started’.


It would add module in all web applications on farm, Check
following entry in web.config:
<add name=”Session”
type=”System.Web.SessionState.SessionStateModule” />

<sessionState mode=”SQLServer” timeout=”60″
allowCustomSqlDatabase=”true” sqlConnectionString=”Data Source=<DatabaseServarName>;Initial
Catalog=SessionStateService_<GUID>;Integrated
Security=True;Enlist=False;Connect Timeout=15″ />


You have to manually update Web.Config file for the specific
SharePoint web application on all servers in the farm.  - <pages
enableSessionState=”true”


By performing above steps you can configure Session State service in your Web Application. Now 
you can user Sessions in ShrePoint without ant issues...