Tuesday, April 07, 2009

Delete a Web Part in SharePoint

If you are planning to remove a web part from a page, ALWAYS click on Delete menu rather than pressing the X button. Clicking on X button would close the web part and you will receive a warning in your servers event log. The web part is not actually deleted from the page, it is removed from the Visible area of the page and placed in the Web Part Page Gallery.

You can delete a CLOSED web part  by using the Web Part Maintenance page. Browse to http://www.yourserver.com/yoursite/yourpage.aspx?contents=1

You could see the web part that has been closed. Now, delete it by selecting the checkbox and pressing Delete

Accessing Current User in MOSS

You could get the current user in SharePoint by calling the following code:

SPUser user = SPContext.Current.Web.CurrentUser;

But to access the current user inside the code running under Elevated Privileges, open new reference to SPSite.

SPSecurity.RunWithElevatedPrivileges(delegate()

{
    using (SPSite site = new SPSite(“http:\\MOSS_SITE”))

{
      using (SPWeb web = site.OpenWeb())

          SPUser user =SPContext.Current.Web.CurrentUser;

    }

}
);

The above code will switch the user context and now, you would get the user under which application pool is running

Mystery of SharePoint\System Account

When you run your code with Elevated Privileges, the code runs under the identity of the hosting application pool. SharePoint\System account exists ONLY within content of SharePoint runtime(not recognised by Windows Security subsystem) and map internally to the account under which application pool is running.

If you are running application pool under DOMAIN\SSUSER account, the code is still audited as running under SharePoint\System account. So, if you get access error on SharePoint site stating that SharePoint\System account don’t have access to the resource, this is because the application pool identity account don’t have access to the resource. Don’t spend time looking for SharePoint\System account :-)

Friday, April 03, 2009

Set Manager Profile property value

To assign manager from one profile to another, you could not set it directly. You need to convert the Source Manager profile to a UserProfile object and then assign the account name to the destination profile manager’s property.

UserProfileManager upManager;
UserProfile sourceProfile,
destProfile;

string managerId = sourceProfile["manager"].Value.ToString();

if (upManager.UserExists(managerId))

{

UserProfile managerProfile = upManager.GetUserProfile(managerId);

destProfile["manager"].Value = managerProfile["Accountname"].Value;

}