Thursday, September 4, 2014

How to resolve "The security validation for this page is invalid" error in SharePoint 2010

How to resolve "The security validation for this page is invalid" error in SharePoint 2010

It may possible many time when you are running custom code in SharePoint's context you are getting the following error:

Microsoft.SharePoint.SPException: The security validation for this page is invalid. Click Back in your Web browser, refresh the page, and try your operation again. ---> System.Runtime.InteropServices.COMException: 

This error can be thrown for different reason but the prominent one that I noticed is when some list/ library edit/ update operation happens in the code and SharePoint tries to protect unsafe updates to its list/ libraries.
To resolve the same error in such cases you need to put your code between the following code stub:



Now rebuild the solution and deploy and test again!

How to add ECB Menu Item for specific SharePoint List or Library

How to add ECB Menu Item for specific SharePoint List or Library

To add ECB Menu Item for specific SharePoint List or Library, first you need to know the Registration ID for that list. To get that, go to the desired List's Settings page and then select Advanced Settings. In that page, enable Management of Content Types and select OK. Now in the List settings page, go to the Content Type Section and select the Content Type listed there.


So in my case, when I selected Document content type, it took me to the next page and in the URL of the next page you have the 'ctype' listed at the end of the url:


Copy that CType as that is going to act as your Registration ID.
Next go to your Visual Studio solution and open Element.xml file. Change the RegistrationType's value to 'ContentType' and place the copied CType value to the 'RegistrationId's value field:


Now just save the file, and deploy the solution from visual studio if working in your development environment or else install and activate the feature to notice the custom ECB Menu only appearing in the specific list :)

How to deploy a WSP to a specific Web Application

How to deploy a WSP to a specific Web Application


Many time there will be requirement to deploy a custom solution to a specific Web Application. But at the time of deployment Power Shell starts throwing the following error:

Install-SPSolution : This solution contains no resources scoped for a Web application and cannot be deployed to a particular Web application. 
At D:\XXX\YYY\DeployWSP.ps1:2 char:19 + Install-SPSolution <<<<  -Identity CustomSPSolution.wsp -webApplication "http://testSPSite" -GACDeployment + CategoryInfo : InvalidData: (Microsoft.Share...InstallSolution: SPCmdletInstallSolution) [Install-SPSolution], SPException + FullyQualifiedErrorId : Microsoft.SharePoint.PowerShell.SPCmdletInstallSolution

From the error message it is clear that SharePoint is looking for some resources specific to the web Application and could not find any. In many cases it is possible that your custom solution don't need any web application specific resources and in those cases we need to fool SharePoint thinking that the WSP has some Web Application specific resources. To do that open you Visual Studio solution and then double click on the Package folder present in the Solution Explorer. Once it is opened, select the Manifest tab and expand the 'Edit Option' present at the bottom of the page. In general Manifest Template going to have following text:


Inside the Solution node paste the following code stub:
   
Change highlighted sections as described and you will be good to deploy your WSP to a specific web application from the Power Shell command prompt.

Tuesday, July 22, 2014

Showing Picture with hyperlink to open link in a new window

Showing Picture with hyperlink to open link in a new window


There are many ways to achieve this functionality –

Adding a Content Editor WebPart and Pasting the following code as mentioned by David Lozzi in his blog:

 

Another way is to create a Site Column of type “Image with formatting and constraints for publishing” in the root site collection. Once you have created this site column, you can add it to any existing list/ library and then at the time of adding an item you can just use this property to add a picture with hyperlink and if you set “Open in a new window” property for the newly added site column, the link will open into a new window.


Thursday, June 19, 2014

How to download all the photos from a dead old giant named ORKUT!!!

Copy - Paste from the following url:
https://support.google.com/orkut/answer/3220186?hl=en

In order to download your Orkut information, you must be the owner of the account. Some Orkut information can be downloaded using Google Takeout in a single ZIP file containing HTML files and JPEG photos.
You can download these items from Orkut:
  • Your profile
  • Scraps received
  • Testimonials received
  • Activities
  • Your photos
Note that you can only export the testimonials and scraps you have received and their comments.
To download your Orkut information:
  1. Go to the Google Takeout page.
  2. Click Choose services and then select Orkut.
  3. Click Create archive.
  4. Wait for the archive to be ready. It may take some time. Optionally, you can be warned by email when it's ready.
  5. When it's ready, click download.
There's no limit on how many times your Orkut information can be downloaded.

Status Indicator webPart not showing status icon

Today I created a SharePoint list (say XYZ) and then a Status List. Then in the Status list I added the SharePoint list XYZ by selecting New > SharePoint List based status indicator. It was working as expected.
Next I went to the Home page of my site and as per the requirement tried to show the current status by editing the page and then adding the Status List WebPart there. But as soon as I finished adding the Status List as a webPart, I noticed the Status Indicator was not showing up. I created new views, changed different settings but of no use. Soon I realized there must be a different way to show the status in the webpart. And that way is to use Status List webPart or Indicator Details webPart from under the Business Data webPart category. Once you add either of the above into the page, it is going to tell you to 'Open the tool pane' to configure it. In the tool pane select your Indicator List i.e., your status list and select Apply > OK and you are done! Save the page, check it in if necessary and now you will see the status icon showing up in your desired page.

Cheers,
Avik

Wednesday, June 11, 2014

ItemUpdating event receiver throwing error - 0x81020015The file has been modified by ...

Few days back I tried to create an event receiver on a SharePoint Library which is going to fire whenever somebody adds a new record. At the time of adding the record the event receiver was supposed to add the logged in users name into a custom column. But to my great amusement it started throwing the following error:

0x81020015The file XXX has been modified by...


Unfortunately I was not able to fix it and after doing a lot of RnD was not able to find a conclusive solution.
It looks like a read/write lock problem and I was not interested in sending a thread to sleep. So I changed my logic a little bit and started implementing the same logic in Item_Updated event. So if you face the same error, then if Googling doesn't help you much, then I will suggest to tweak the logic and use Item_Updated event! Sorry!

How to show values on mouse hover on a graph present in SharePoint Chart WebPart

To show values on mouse hover on a graph present in SharePoint Chart WebPart, do the following:

1. Add a Chart Web Part from Business Data Category
2. Select the Date and Appearance link present on the newly added Chart webPart
3. Select Customize your Chart
4. On the left side Chart Customization Wizard, expand Step 3 and select Hyperlinks and Tooltips
5. On the Series Tooltip box enter #VALX [#VALY] and select Finish
6. Stop Editing or Check In the page

Now hover your mouse on the graph and you will see value at those point in X,Y manner.

Monday, May 12, 2014

"PowerPoint Web App encountered an unexpected error" - Getting this error?

Few days back an end user of my organization started getting the the following error whenever she tried to open any PowerPoint presentation after clicking the link from within any SharePoint document library:


Initially we though it is related to Office Web App feature, but after checking all the features and permissions we were not able to find anything wrong. When we were out of options, we found out that the user has ran the Software Upgrade Suite and then we noticed she was using IE 9 64 bit. Then we thought to try it in a 32 bit version of IE and voila! So if you are also experiencing the same issue, try it on a 32 bit browser!

Thursday, August 11, 2011

Assigning SharePoint List Item level permission programmatically


To add item level permission in an SharePoint List or Library, you need to keep three things in mind:

You have a valid SPUser object in your hand
You have to break the role assignment inheritance for the list
You have to add Role Definition and Role Assignment to the targeted list item

Though above statements looks complicated, don't think much about them – just use following two functions wisely and you are done :)

Assumption:

1. You have SPListItem object in your hand
2. You have a valid SPUser in your hand

Working:

First call the below method and pass the SPListItem as the input parameter:

RemoveAllPermissions Function
  1. private static void RemoveAllPermissions(SPListItem CurrentlistItem)
  2.         {
  3.             //The below function Breaks the role assignment inheritance for the list and gives the current list its own copy of the role assignments
  4.             CurrentlistItem.BreakRoleInheritance(true);
  5.             //Get the list of Role Assignments to list item and remove one by one.
  6.             SPRoleAssignmentCollection SPRoleAssColn = CurrentlistItem.RoleAssignments;
  7.             for (int i = SPRoleAssColn.Count - 1; i >= 0; i--)
  8.             {
  9.                 SPRoleAssColn.Remove(i);
  10.             }
  11.         }

Next call the below method and pass the desired parameters:

GrantPermission Function
  1. private static void GrantPermission(SPListItem CurrentListItem, SPWeb oSPWeb, SPRoleType SPRoleType, SPPrincipal SPPrincipal)
  2.         {
  3.             try
  4.             {
  5.                 //Create one Role Definition i.e Full Controls, Contribute rights or Read rights etc.
  6.                 SPRoleDefinition oSPRoleDefinition = oSPWeb.RoleDefinitions.GetByType(SPRoleType);
  7.                 //Create one Role Assignment for the specified SP user or group.
  8.                 SPRoleAssignment oSPRoleAssignment = new SPRoleAssignment(SPPrincipal);
  9.                 //Bind the role definition to the role assignment object created for the user or group.
  10.                 oSPRoleAssignment.RoleDefinitionBindings.Add(oSPRoleDefinition);
  11.                 //Add it to the specified list item.
  12.                 CurrentListItem.RoleAssignments.Add(oSPRoleAssignment);
  13.                 //update the list item so that specified user assignment will have the access.
  14.                 CurrentListItem.Update();
  15.             }
  16.             catch (Exception ex)
  17.             {
  18.                 EventLog.WriteEntry("Error in UAR Initiation Workflow", "GrantPermission() : " + ex.Message);
  19.             }
  20.         }

Thats it!!! So simple and so easy.
For your easy reference I am including the code block from where I used to call these functions:
 

How I am Calling them
  1. if (validUsername)
  2.                             {
  3.                                 if (rdr["Manager Logon"] != null)
  4.                                 {
  5.                                     SPUser CurrentUser = mySite.EnsureUser(rdr["Manager Logon"].ToString());
  6.                                     RemoveAllPermissions(item);
  7.                                     GrantPermission(item, mySite, SPRoleType.Contributor, CurrentUser);
  8.                                     SPGroup oGroup = mySite.SiteGroups["UARAdministrators"];
  9.                                     GrantPermission(item, mySite, SPRoleType.Administrator, oGroup);
  10.                                 }
  11.                             }
  12.                             else
  13.                             {
  14.                                 //Admin need to resolve this issue in the list
  15.                                 RemoveAllPermissions(item);
  16.                                 SPGroup oGroup = mySite.SiteGroups["UARAdministrators"];
  17.                                 GrantPermission(item, mySite, SPRoleType.Administrator, oGroup);
  18.                             }

Cheers,
Avik