Thursday, 31 July 2014

Provisioning XsltListViewer web part in site definition / Issue with XsltListViewer web part order


Hi All,
Today sharing one small thing but interesting one. I learned this new thing so sharing J


Scenario: We have our custom site definition. We are provisioning instance of Link list in <Lists> element in site definition
We need to add XsltListViewer web part (XsltListViewWebPart) on front page for the above Link list. So we have module element to provision the front page and added XSLT Viewer web part having order 2. This module is added into web level feature and not in Onet.xml.

Problem: Whenever we were creating site this XsltListViewer web part appearing over the top means having order 0 for it. It’s not coming over the order mentioned for it, i.e. 2.

After doing lots of research it’s found that this is related to order of execution in Onet.xml. In following order parsing of Onet.xml happens:

  1. Site Features

  2. Stapled Site Features

  3. Web Features

  4. Stapled Web Features

  5. <Lists> element

  6. <Modules> element
Now, we are provisioning a page and web parts on it using web level feature so whenever page is provisioned, SharePoint trying to add XsltListViewer web part for the list which is not provisioned yet. <Lists> element executes after the <WebFeatures>.

But surprisingly SharePoint doesn’t throw an exception over there or doesn’t fail the site creation process but latter adds it as a top web part in the respective zone i.e. having order 0.

Possible approaches to avoid this kind of problem:


1.    Provision the list instance in web level feature and set the dependency of this feature over the feature which provision the pages.
 
    We used this approach and the web part is placed on correct order.
 
      2. Use the <Module> element from Onet.xml itself for provisioning the pages since 
          <module> element executes lastly. Till the time <Lists> are provisioned.


References:

http://social.msdn.microsoft.com/Forums/office/en-US/9387c512-032d-4db6-b893-e0e91662c144/caml-execution-sequence?forum=sharepointdevelopmentlegacy

http://stefan-stanev-sharepoint-blog.blogspot.fi/2010/12/feature-stapling-in-sandbox-solutions.html
Thanks!

Enjoy reading J
Feel free to comment / feedback if any or if you have any query J

Saturday, 19 July 2014

Difference between Search Result web part and Content Search web –part III: Result Types

Hi Again,

Today, I’ll discuss about the Result Types. If we do little bit googling we will get very good articles / blogs on Result Types.

Few good references for details of Result Types:





So here I’ll not go in delve of Result Types but will discuss the use case (scenario) we have and how we implemented with the help of Result Types.

Use Case: We have a portal and which contains many article pages, news pages and links. We have requirement like on our main portal home page we need to show the 5 article pages, news pages and links. So total 15 items. All we need to show in such way like have common background for all 15 items without any gap in between different type of result but different presentation to identify those. Like before link there should be “>>”, news pages item should show the modified date and article pages item should show the author.

Like as shown in following example, with yellow background (this is just exampleJ)

Article1 Author
Article 2 Author
Article 3 Author
Article 4 Author
News Item 1 Modified date
News Item 2 Modified date
News Item 3 Modified date
News Item 4 Modified date
>> Link1
>> Link2

Different Approaches:

We have decided to implement this requirement with Search, so in any approach we will require at least three Display Templates. 

1. Initial thought in our mind is 3 Content Search web parts adding in one zone and in page layout add this zone in Div tag so that we can have common background for all the web parts. But this approach has one drawback. We need to add three Content Search web parts. On our portal already we have 4-5 search web parts so we are little bit concerned about performance and then maintenance. Need to maintain the three web parts.

     2. Second approach, my team member suggested the Result Types. So we just need to create three result types and set rules like if Article page content type then apply article page display template, if News Article content type then apply news display template and if link content type then apply link display template. And then we will require only one Search Result web part and need to configure the Search result web part to use Result Types as follow:
Fig 1. Configuring Result Types in Search result web part

So here at least our performance and maintenance related problem is solved since we are using only one Search Result web part.

So basically here I want to focus on Result Types which are used to display result differently. But Result Types are available only with Search Results web part.  

[Even though we will go with Content Query web part (CQWP) for this requirement, we will require three CQWP need to add in one Zone same as Content Search web part.]

Thanks!

Enjoy reading J

Feel free to comment / feedback if any or if you have any query J





Thursday, 10 July 2014

Access denied error while activating “Following Content” feature

Hi All,

Its long time back again, too busy on project work and now I have long list of topics ready for sharing.

This post is related to activating “Following Content” web level feature.




Our customer reported the bug that they are getting an Access Denied error when they are trying to activate the “Following Content” feature.



We started the investigation; User who is trying to activate this feature has Site Collection Administrator rights. So this means even Site Collection Administrator couldn't activate this feature.  

So we started digging the ULS logs and found following exception related to this feature:

<nativehr>0x80070005</nativehr><nativestack></nativestack>Access is denied.

SPRequest.GetFileAndFolderProperties: UserPrincipalName=i:0).w|s-1-5-21-962155459-1177681987-1237804090-74654, AppPrincipalName= ,bstrUrl=http://<my site>/personal/<user account> ,bstrStartUrl=Social/Private/FollowedSites ,ListDocsFlags=24584 ,bThrowException=False       a0fa9d9c-f590-6094-c775-fb7648d76852
System.UnauthorizedAccessException: <nativehr>0x80070005</nativehr><nativestack></nativestack>Access is denied., StackTrace:    at Microsoft.SharePoint.SPWeb.GetFileOrFolderProperties(String strUrl, ListDocsFlags listDocsFlags, Boolean throwException, SPBasePermissions& permMask)     at Microsoft.SharePoint.SPFolder.PropertiesCore(Boolean throwException)     at Microsoft.SharePoint.SPFolder.get_Exists()     at Microsoft.Office.Server.UserProfiles.SPSocialDataStore.EnsureNamespaceExists(String ns, String scopeName, Boolean secured)     at Microsoft.Office.Server.UserProfiles.SPSocialDataStore.WriteFollowedItem(FollowedItem item, FollowedItemData data)     at Microsoft.Office.Server.UserProfiles.SPSocialDataStore.Follow(FollowedItem item, FollowedItemData data, Boolean checkLimit)     at Mic...    a0fa9d9c-f590-6094-c775-fb7648d76852
...rosoft.Office.Server.UserProfiles.FollowedContent.FollowItem(FollowedItem item, Boolean isInternal)     at Microsoft.Office.Server.UserProfiles.FollowedContent.Follow(Uri url, FollowedItemData data)     at Microsoft.SharePoint.Portal.ContentFollowingFeatureReceiver.AutoFollowWeb(SPWeb web)     at Microsoft.SharePoint.Portal.ContentFollowingFeatureReceiver.FeatureActivated(SPFeatureReceiverProperties properties)     at Microsoft.SharePoint.SPFeature.DoActivationCallout(Boolean fActivate, Boolean fForce)     at Microsoft.SharePoint.SPFeature.Activate(SPSite siteParent, SPWeb webParent, SPFeaturePropertyCollection props, SPFeatureActivateFlags activateFlags, Boolean fForce)

After digging into these logs it seems that UnauthorizedAccessException coming while accessing the Social list from my site of one of the user account. We are wondering why it goes to access the Social list of another accounts my site and not the current users my site who trying to activate the feature.


When we are logged in with account which is given in exception and trying to activate the feature, feature activated successfully. We are really wondering what is happening, actually user who have proper permissions to the web could activate the feature.

So to check what exactly happens when this feature is activated, took my best friend ILSpy help. Following is the code for FeatureActivated method



 It calls the method AutoFollowWeb(). Following is the code of this method:



UserProfile userProfile = userProfileManager.GetUserProfile(web.Author.LoginName);
       SPUserToken userToken = web.Author.UserToken;
       FollowedContent followedContent = new FollowedContent(userProfile, context, userToken)

Notice the line marked in red. It gets the object of the Author of the web means user who has created the web and not the current user who is activating the feature.

So this means that not any user except, user who has created the web (author of the web) can activate this feature.

Few takeaways:

  1. Only user who creates the web (author of the web) can activate this feature. All the other users are not able to activate this feature.
  2. Whenever “Following Content” feature is activated on any web, web is automatically followed for the author of the web. 
  3. Once feature is activated by Author of the web, then other users can follow the web. When user follows the web, one entry to Social list is made, this list is available in users my site.

 That’s all here for now

 Thanks!     

 Enjoy reading J


 Feel free to ideas / comment / feedback if any or if you have any query J