Pages

Showing posts with label SharePoint Features. Show all posts
Showing posts with label SharePoint Features. Show all posts

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!

Saturday, September 12, 2009

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!

Sunday, August 23, 2009

How to assign lookup values into List definition – schema.xml file

I had requirements in which I wanted to deploy my custom lists from one server to another server, I had generated my schema and forms files using solution generator tool, now when I open one of my generated schema file in which I have lookup columns linked to another custom list, I have observed that solution generator tool commented custom lookup columns, I mean it didn’t figured out which custom list to attached for lookup columns.

There are many ways to achieve these challenges, one way would be if we know GUID of custom list which we are going to look up on then we can write same GUID into list definition schema file, but in my case all custom list was deployed at same time, so I don’t know what’s GUID of lookup list.

Another solution I found was, we can write feature event receiver on custom list, so when feature install/deploy custom list at same time we can take lookup column list name and assign into lookup columns into list definition file.

But at the last I have found simplest solution to assign lookup values into list definition, there is property called "LIST" into fields columns for schema.xml file, so in same list definition file, we can write List="Name of List" for lookup columns, so it will attached lookup list name for columns.

I hope that above information makes some sense to reader, but if it’s too confusing then please let me know about your challenges we can work out :)

Enjoy!

Monday, July 20, 2009

How to give intelligence into feature XML file

One day I was creating my feature file into solution package, at that time I saw that I didn’t get intelligence of all elements and attribute into my file and I have found that by adding below schema file, we will be having intelligence in any XML file. Though it’s very small things, but I thought to share with you.

Schema file:

"C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\TEMPLATE\XML\wss.xsd"

Error while deploying solution “This solution contains no resources scoped for a Web application and cannot be deployed to a particular Web application”

I was getting error while deploying solution through STSADM command, error was This solution contains no resources scoped for a Web application and cannot be deployed to a particular Web application”

I have figured out that it’s giving me problem because of URL parameter into deploysolution STSADM command, after removing it from command, my solution package was working fine.

Reference Link:

http://snahta.blogspot.com/2008/08/this-solution-contains-no-resources.html

Saturday, October 18, 2008

SharePoint Custom Actions

I came across requirement of customizing out of box SharePoint pages (like create.aspx, spcf.aspx, qstnew.aspx) to fulfill client need to add functionality or custom code in those pages.

Generally we are making copy of those pages for making changes and add custom link on page from where it’s getting called.

Example:

If you are required to override the Web Part Page/Survey Creation Page (spcf.aspx/new.aspx) then you have to add link under particular Group in Create.aspx page. This you can achieve without writing any code in create.aspx by creating Feature using Custom Actions.

SharePoint provides so many defaults Custom Actions which we can use as per our creativity.

Code example: To add custom link in Site Actions Menu which takes user to custCreate.aspx page.

<CustomAction
Id = "RepLetterCustCreatePage"
Title = "Custom Create"
Sequence="2000"
Description = "Add a new library, list or web page to this website using custom Create Page."
GroupId = "SiteActions"
Location = "Microsoft.SharePoint.StandardMenu">

<UrlAction Url="~site/_layouts/custcreate.aspx"/>

</CustomAction>

Example :

I also added custom link into SharePoint Central Admin Site into Operation TAB, on clicking of new added link, I am opening my ASPX page from Central Admin site.

Element file look like:

<Elements xmlns="http://schemas.microsoft.com/sharepoint/">

<CustomActionGroup
Id="CustomSecurity"
Location="Microsoft.SharePoint.Administration.Operations"
Title="Custom Security"
Sequence="1000"
/>

<CustomAction
Id="TerminationJob"
GroupId="CustomSecurity"
Location="Microsoft.SharePoint.Administration.Operations"
Sequence="10"
Title="Termination Job">

<UrlAction Url="/_admin/TerminationJob.aspx"/>

</CustomAction>

</Elements>

One more thing, I remember that after developing feature, I was not able to see custom link into Central Admin site, I came to know that in feature file, where we are defining element manifest file, I use wrong tag for giving XML file location, we have to use

<ElementManifests>

<ElementManifest Location="elementManifest.xml" />

</ElementManifests>

I was using

<ElementFile Location="elementManifest.xml" />

Useful reference link for Custom Actions:

http://msdn.microsoft.com/en-us/library/bb802730.aspx

http://msdn.microsoft.com/en-us/library/ms473643.aspx