Pages

Friday, December 11, 2009

How to stop event handler recursion

I was getting errors into event log when I was trying to update list in which I have written event handler code and again that code was executed from event receiver only, so it goes in loop, sometimes my page becomes very slow.

Solution:

I need to write code which will disabled event firing before when I was trying to update something into same list and then I need to enable event firing after my code.

SAMPLE CODE:

this.DisableEventFiring();

objtem.SystemUpdate(false);

this.EnableEventFiring();

Reference Links:

 

http://www.sharepoint-tips.com/2006/10/preventing-event-handler-recursion.html

Thank you!

How to impersonate user into event receiver class or Access denied error on event receiver

Requirement:

I need to remove user permission from list item based on some specified condition; normal user don’t have rights to remove user level permission, so I need to put my business logic into RunWithElevatedPrivileges delegate into event receiver class, but I was facing some challenges after that also, normal user was getting access denied errors while breaking inheritance of list item into event receiver class.

Solution:

We need to be very careful while writing impersonation code into event receiver, because if we miss one class or object to take reference from current logged in user then code won’t work, in my case – I was taking List Item on current logged in user context, I need to be very specific for taking List Item. Key here is needs to take list item by SPListItem objItem = elevatedWeb.Lists[properties.ListId].GetItemById(properties.ListItem.ID);

 

SAMPLE CODE:

 

SPSecurity.RunWithElevatedPrivileges(delegate()

            {

                using (SPSite elevatedSite = new SPSite(properties.SiteId))

                {

                    using (SPWeb elevatedWeb = elevatedSite.OpenWeb(properties.RelativeWebUrl))

                    {

                        SPListItem objItem = elevatedWeb.Lists[properties.ListId].GetItemById(properties.ListItem.ID);

  objItem.Web.AllowUnsafeUpdates = true;

                                objItem.BreakRoleInheritance(false);

                            objLeaseItem.Web.AllowUnsafeUpdates = true;

}

                    }

                });

Reference Links:

http://social.msdn.microsoft.com/forums/en-US/sharepointdevelopment/thread/f2ccd61a-8828-4c17-8360-20d45d6b9514

http://social.msdn.microsoft.com/Forums/en-US/sharepointdevelopment/thread/c3d2b304-7fcc-40d2-86ce-61d9b21b03d7  

http://boris.gomiunik.net/2009/04/spsecurityrunwithelevatedprivileges-and-access-denied-error-on-event-receiver/  

Good Luck!
I had a requirement in which user can cancel / terminate workflow from custom ASPX page, I got good reference from this site http://blog.brianfarnhill.com/2008/10/01/how-to-cancel-a-workflow-programmatically/ regarding how to cancel workflow programmatically, but what I had discovered was that normal user were getting error while canceling workflow. User who has full control rights into site, they can able to cancel workflow without any errors.

ERROR:   

Value does not fall within the expected range.   at Microsoft.SharePoint.Library.SPRequestInternalClass.CancelWorkflow(String bstrUrl, String bstrListName, Guid workflowInstanceId)

Solution:

It’s because of user permission level, from out of the box workflow status page also, contributor user don’t have permission to “Terminate this workflow” functionality, so we need to put Cancel workflow logic into RunWithElevatedPrivileges delegate code. After that normal user can also cancel workflow without any errors.

SAMPLE CODE:

SPSecurity.RunWithElevatedPrivileges(delegate()

                {

                    using (SPSite site = new SPSite(SPContext.Current.Site.ID))

                    {

                        using (SPWeb web = site.OpenWeb(SPContext.Current.Web.ID))

                        {

                                                            //object model code for cancelling workflow

                                   }

                    }

              });

Enjoy!

Saturday, September 12, 2009

ASP.NET Grid View

In this article, I’ll talk about and will give you reference links of simple ASP.NET grid view requirements which we are facing in day to day coding life :)

How to put radio button into ASP.NET Grid View:


http://techahead.wordpress.com/2008/04/01/aspnet-using-radio-button-inside-grid-view/


How to pass value from child window to parent window using JavaScript:



http://www.plus2net.com/javascript_tutorial/window-child3.php



How to pass data of child page grid view to parent window:



http://www.codeproject.com/KB/webforms/DataGrid_Row__Value.aspx



How to click button event on entering values into textbox and pressing enter key:



http://forums.asp.net/t/1361883.aspx



How to get readonly values from asp.net textbox to server side:



http://forums.asp.net/t/1460570.aspx



http://geekswithblogs.net/ranganh/archive/2007/05/10/112390.aspx

Object reference not set to an instance of an object. The Solution installation failed

I was getting error "Object reference not set to an instance of an object. The Solution installation failed" while adding and deploying my solution package from STSADM command, I have looked into my event log and SharePoint log to find out the exact error, I have found out that it’s related to Admin Content SQL database.  The current user who is executing the STSADM command must have full control access to this content database.

Then I run my solution package with service account and it ran well without any problem, here we can also add current user into content database and will give full control rights to current user to executing solution package.

Good Luck!