Code Project

Link Unit

Showing posts with label WebPart. Show all posts
Showing posts with label WebPart. Show all posts

Monday, March 22, 2010

DoesUserHavePermissions in WebPart

I wrote a search webpart and it need check if the current user has the right to some lists so I can highlight those lists in different color , but Whenever SPList.DoesUserHavePermissions is called on the object[List/Library], for the user who does not have the permission, it should return false, but it gives an exception. An IE browser pops up asking for user to type in username and password. This dialog box should not appear as my code catches the exception.
Solution: After googling around for some solution on the same line , found points which may help fellow visitors.
#1 DoesUserHavePermissions only works in WSS, not SPS
#2 Suppress Popup for Unauthorized Access:The problem happens with your SPSite object from which you get the web, then the list, there is a property called CatchAccessDeniedException, you should set it to false, then make your call, end the popup will not show up. This is a standard WSS feature, and will happen every time you try to access a resource to which the current user does not have access.
WorkAround : Use the following in your webpart

=====
using(SPWeb oWeb=SPContext.Current.Web)
{
oSite.CatchAccessDeniedException = false; // Suppress the popup
SPListCollection oList = oWeb.Lists;
oList.ListsForCurrentUser = true;
int iCount = oList.Count;
for(int i=0;i{
try
{
if(oList[i].Permissions.DoesUserHavePermissions(SPRights.ViewListItems)) // Check for view Rights
{
// Do something
}
}
catch(System.UnauthorizedAccessException ex)
{
//Do Nothing, or as you please
}
}
}
=====


Hope this helps

Friday, September 04, 2009

This solution contains two assemblies with the same name, or the SharePoint server already has an assembly with the specified name.

While trying to redeploy the same WebPart ,I was facing this rather silly error. I tried to do the “cleanup” by right clicking the Project and then redeployed same error appeared. Later figured it out by throwing a search in Google



#1 See if the feature that you're trying to add still exists in the "\Program Files\Common Files\Microsoft Shared\web server extensions\12\TEMPLATE\FEATURES" path. Delete it.


#2 Uninstall the particular Assembly from c:\windows\assembly folder too.


#3 Try to deploy it again.

Design decision to use ascx control in sharepoint webpart versus pure sharepoint webpart


1. Create asp.net webusercontrol (.ascx), and then load that user control in the newly created webpart.

2. Create a webpart from scratch, by putting the code to generate each and every control in code.

if you need to create a control with a really advanced user interface, for example many controls and stuff, then a user control could be much easier to create.

I have found that the execution lifecycle (control viewstate, event bubbling, etc.) is a pain when going with Option 2. I choose Option 1 because the viewstate is easily managed in the code. Additionally, I like Option 1 because I can visually design the page using the VS toolbox to drag and drop controls on the design surface as opposed to Option 2 where you have to handle each control manually in code.

The approach 2 may sound difficult because of not very great integration with Galleries , but if you have the luxury of invest the time, you'll be better off in the long run.

Wednesday, September 02, 2009

The feature name WebPart1 already exists in SharePoint. You need to rename the feature before solution deployment can succeed

While working in Visual Studio 2008 , we were able to create new WebParts and deploy them easily . Off late we started facing the following problem.

“The feature name WebPart1 already exists in SharePoint. You need to rename the feature before solution deployment can succeed.”

The error may seem stupid , the solution to it is simple.

A simple method to avoid the above error is as follows
  1. Immediately after creating a new webpart project, remove the Webpart1 folder completely.

  2. Add new web part to the project by right clicking on the project and selecting new item from the context menu.

  3. In the Add new item dialog box select Sharepoint from categories and Select Web Part from templates.

  4. Give a name for your webpart and click Add button.