Tuesday, April 6, 2010

Enabling Session State in SharePoint 2010

Did you got the following error when using session state in SharePoint 2010?
"Session state can only be used when enableSessionState is set to true,
either in a configuration file or in the Page directive. Please also make
sure that System.Web.SessionStateModule or a custom session state module"



I have the solution for you. you have to add SessionStateModule to your site's web.config file as follows :

1.Open IIS 7 manager and find your web application.
2.Double click "Modules" in the IIS.
3.Click "Add Managed Module" on the right side.
4.In the Add Managed Module dialog, enter "SessionState" or any name you like, and choose the following assembly from the dropdown:
System.Web.SessionState.SessionStateModule, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a

enjoy !

Monday, April 5, 2010

Unable to open FBA (Form Based Authentication) Site from SharePoint Designer

There are two ways to open FBA Site from SD :

1. Authenticate from IE First : Open your FBA site from IE, Login as Administrator and choose to "Login Automatically" , then open the site from SharePoint Designer. The SharePoint Designer will uses the authentication cookie to open your FBA site.


2. Extend your FBA site from the central administrator. this will create another IIS website with a windows authentication by default. open the site using SD by providing the windows auth. URL of your site.

Sunday, December 27, 2009

HttpContext & SPContext is always null when Uploading Multiple document libraryin sharepoint

HttpContext & SPContext is always null when Uploading Multiple document libraryin sharepoint : I'm tring to get a value from the session and update a custom field when uploading multiple documents to my sharepoint document library.

I faced an issue when I was tring to update some custom fields of my document library when uploading multiple documents, the field was ( ProjectID ) which I put it inside a session in my custom webpart (the step before uploading the documents).

What I did is : I put the projectID into the cache ( per user which acts as a session) inside the custom webpart as follows :

if (Request.QueryString["ProjectID"] != null)

{

HttpRuntime.Cache.Remove(SPContext.Current.Web.CurrentUser.LoginName);

HttpRuntime.Cache.Add(SPContext.Current.Web.CurrentUser.LoginName, ProjectID, null, DateTime.UtcNow.AddMinutes(60), System.Web.Caching.Cache.NoSlidingExpiration, System.Web.Caching.CacheItemPriority.Normal, null);

}

then I implemented the ItemAdded event and I get the value of the cached projectId through :

Note that I get the cached key through the created by field which is the same key name that I cached it from inside my custom webpart.

public override void ItemAdded(SPItemEventProperties properties)

{

try

{

string ProjID = "";

string CreatedBy = null;

if (properties.ListItem["Created By"] != null)

CreatedBy = properties.ListItem["Created By"].ToString().Split(';')[1].Replace("#","");

if (HttpRuntime.Cache[CreatedBy] != null)

{

//SPContext.Current.Web.CurrentUser.LoginName;

ProjID = HttpRuntime.Cache[CreatedBy].ToString();

if (properties.ListItem["Project"] == null)

{

properties.ListItem["Project"] = new SPFieldLookupValue(ProjID);

properties.ListItem.SystemUpdate();

}

base.ItemAdded(properties);

}

}

catch (Exception ex)

{ }

}

Regards,

Mohammed Barakat Kharboush

Monday, October 13, 2008

MCTS 70-542

I passed the MCTS exam for SharePoint 2007 - Application Development today :)

Wednesday, July 9, 2008

Using Ajax Technology & RadControls with MOSS 2007 Site

In this post we will take about how to Enable Ajax and Use RadControls in your sharepoint site.

*) Enable Ajax in your Sharepoint Site :

Two ways to do it.

First way:

This way will enable Ajax 3.5 in your sharepoint site using VS 2008.
  • Open Vistual Studio 2008.
  • Go to File --> Open --> Web Site
  • Browes to your MOSS 2007 site location, for example :

C:\inetpub\wwwroot\wss\VirtualDirectories\90
supposing your site's port number is 90.

  • Upgrade message will appear asking you for updating your site from .Net Framework 2.0 to .Net Framework 3.5.

  • Click Yes to Upgrade your site to .Net Framework 3.5
  • Open web.config file and you will see the changes that you need for enableing Ajax into your site automatically there .

Second way :

In this way we will follow the normal way to enabel Ajax in any ASP.NET application.(manually).

Note : if you choose to install AJAX 3.5 , you have to replace the assembly version from Version=1.0.61025.0 to Version=3.5.0.0

*) Using RadControls in MOSS 2007 Site.

To use RadControls in MOSS 2007 site , follow the following steps.

  • Open your MOSS 2007 Site with VS 2005 , 2008.

C:\inetpub\wwwroot\wss\VirtualDirectories\90

supposing your site's port number is 90.

  • Add RadControls to your ToolBox using Telerik.Web.UI.dll file.
  • Create new ASPX page inside a folder for example MyPage folder.
  • Test RadControls by Drag and Drop a Script Manager OR RadScriptManager and RadAjaxManager OR RadUpdatePanel to your page and any other RadControls.

Note : Till this moment, if you test your page from the browser, you will get a security exception becos RadControls should run under a Full Trust.

  • deploy Telerik.Web.UI.dll assembly into the GAC and everything will work fine

enjoy :)

Thanks,

Mohammed Barakat Kharboush

401 unauthorized error when accessing "MySite" in MOSS 2007

Problem :

I installed MOSS 2007 and after creating my first web application and the SSP (Shared Service Provider), When I try to access "MySite" I was receiving a credential prompt, although I entered the correct username & password three times I was getting the a 401 unauthorized error.

Solution :

From the Central Admin, I deleted the site collection that host mysite and then I create it again using My Site Host template which can be found under the Enterprise tab in creating new site collection page and everything works fine.

Thanks,
Mohammed Barakat Kharboush

Monday, June 30, 2008

MOSS 2007 : Access Denied exception even when using SPSecurity.RunWithElevatedPrivileges Method

Problem :

if you implement my previous post which is MOSS 2007 : Implement Task - Task Activities (Parent-Child Relationship) on the same List and after attach the mentioned list event handler on ItemAdded, you will get an "Access Denied Exception" if the current loged in user is not one of the MOSS site's owners (if he is not included in the site's owners group) even if you use SPSecurity.RunWithElevatedPrivileges WSS 3.0 API Method. let me show you the code.

//////////////////////////////////////////////////////////////////////////

public override void ItemAdded(SPItemEventProperties properties)
{
using (SPWeb web = properties.OpenWeb())//Open the web you want to use
{
SPContentType TaskCT = web.ContentTypes["TaskCT"];

if (TaskCT != null)
{
// Check if the Added Item Belongs to Task Content T ype
if (properties.ListItem.ContentType.Name == TaskCT .Name)
{
SPList TaskList = web.Lists[properties.ListId];

// Get folder in the list and a content type.
SPFolder TaskFolder = TaskList .RootFolder.SubFolders[properties.AfterProperties["Title"].ToString()];
SPContentType TaskActivityCT = TaskList .ContentTypes["TaskActivityCT"];
List contentTypes = new List();
contentTypes.Add(TaskActivityCT);

// Set the content type order for the Task Folder and update
TaskFolder.UniqueContentTypeOrder = contentTypes;
TaskFolder.Update(); // The Exception Will Occur Here (Access Denied)
}
}
}
}

//////////////////////////////////////////////////////////////////////////


The above code will generate an exception TaskFolder.Update(); line which marked with red color. why?

Becos as we said, the loged in user is not a member of the site's owner group.

What we can do?? do I have to use SPSecurity.RunWithElevatedPrivileges Method that will Execute my method with Full Control rights even if the user does not have?

Solution:

if you use SPSecurity.RunWithElevatedPrivileges method with the above code, the exception will still raising again and again when your code executed by a normal user (not site owner) ! that will make me lose my mind!!! why ?

lets try it

//////////////////////////////////////////////////////////////////////////

public override void ItemAdded(SPItemEventProperties properties)
{
SPSecurity.RunWithElevatedPrivileges(delegate()
{

using (SPWeb web = properties.OpenWeb())//Open the web you want to use
{
SPContentType TaskCT = web.ContentTypes["TaskCT"];

if (TaskCT != null)
{
// Check if the Added Item Belongs to Task Content T ype
if (properties.ListItem.ContentType.Name == TaskCT .Name)
{
SPList TaskList = web.Lists[properties.ListId];

// Get folder in the list and a content type.
SPFolder TaskFolder = TaskList .RootFolder.SubFolders[properties.AfterProperties["Title"].ToString()];
SPContentType TaskActivityCT = TaskList .ContentTypes["TaskActivityCT"];
List contentTypes = new List();
contentTypes.Add(TaskActivityCT);

// Set the content type order for the Task Folder and update
TaskFolder.UniqueContentTypeOrder = contentTypes;
TaskFolder.Update(); // The Exception Will Occur Here (Access Denied)
}
}
}

});
}

//////////////////////////////////////////////////////////////////////////
we will take now about the reason, why we still get that exception even after using SPSecurity.RunWithElevatedPrivileges method.

from the above code we are using properties.OpenWeb() Method which returns SPWeb object that refer to the current web which our code execute in, but that object was created in a context of a non-administrative user. lets solve it now :


//////////////////////////////////////////////////////////////////////////

public override void ItemAdded(SPItemEventProperties properties)
{
SPSecurity.RunWithElevatedPrivileges(delegate()
{

SPWeb webInContext = properties.OpenWeb();
SPSite siteInContext = webInContext.Site;
Guid _webGuid = webInContext.ID;
Guid _siteGuid = siteInContext.ID;

using (SPSite site = new SPSite(_siteGuid))
{
using (SPWeb web = site.OpenWeb(_webGuid ) )//Open the web you want to use
{
SPContentType TaskCT = web.ContentTypes["TaskCT"];

if (TaskCT != null)
{
// Check if the Added Item Belongs to Task Content T ype
if (properties.ListItem.ContentType.Name == TaskCT .Name)
{
SPList TaskList = web.Lists[properties.ListId];

// Get folder in the list and a content type.
SPFolder TaskFolder = TaskList .RootFolder.SubFolders[properties.AfterProperties["Title"].ToString()];
SPContentType TaskActivityCT = TaskList .ContentTypes["TaskActivityCT"];
List contentTypes = new List();
contentTypes.Add(TaskActivityCT);

// Set the content type order for the Task Folder and update
TaskFolder.UniqueContentTypeOrder = contentTypes;
TaskFolder.Update(); // The Exception was gone :)
}
}
}

}
});
}

//////////////////////////////////////////////////////////////////////////

in this way, the SPWeb is created in a context of an administrative user( site.OpenWeb(_webGuid ) )

enjoy :) and don't forget to include a reference to System.Configuration namspace to have the Guid available.

Thanks,
Mohammed Barakat Kharboush