Pages

Showing posts with label GUID. Show all posts
Showing posts with label GUID. Show all posts

December 28, 2016

Search cannot see some metadata tagged documents SharePoint

Greetings to fellow SharePoint developers and admins.. here is one more issue about crawling managed metadata terms and how to solve it.


Background:
The objective was to have a custom document search by terms. In SharePoint 2013, we have a document library with couple of managed metadata fields (some are single while others are multi value). Based on some logic, a custom code solution will assign (tag) term values to documents. Tagged documents are ultimately to be found using a custom search page by passing 2 term names and their Guids. Example:

/Pages/customsearch.aspx?Sites=5915df2d-f1e7-4247-90ff-9b55a391afc1&SiteCategory=3edf1303-7fd3-497d-a23e-bceb01015f8c

The search page contains Content Search Web Parts. Each one contains a custom query that uses query strings to retrieve all items tagged with multiple values of either or both of the 2 terms used. Example:

{|owstaxIdSites:{QueryString.Sites}*} {|owstaxIdSiteCategory:{QueryString.SiteCategory}*}


The Issue:
The CSWP query show some documents, but do NOT show other documents even if they were tagged with exactly the same terms.

Here is the list of symptoms:
- Document item is tagged with valid terms (we can see/edit the terms with no issue in SharePoint UI in DispForm/EditForm views respectively on all fields)
- Document is crawled successfully and can be viewed by default search page when search by title
- No issue from crawl log regarding said documents themselves nor their metadata terms
- If I just just edit the document and Save, then run crawl it might fix the issue for some documents!
- Also, if I call SystemUpdate() on item from code (without making any other changes), then crawl it will be fixed as well!
- I noticed that for affected documents, all terms from all metadata fields are not showing under under any query.


Failed attempts: 
Tried the following methods,but Non of the below worked:
- Tried re-run Incremental Crawl.
- Tried partial re-index target site or content source (to force search to consider documents as 'dirty' as if the property 'vti-searchversion' was updated) then run Incremental-Crawl.
- Tried Full crawl.

Hmm, so it's not about the search crawl, not the document, not the terms, but something with how the terms are stored in metadata columns. But from the UI, everything seems normal..


Root Cause:

So I paid another visit to the custom code part. After careful checking, I found a bug in code where there are cases where it inserted multiple copies of the same term in one of the fields.

Example: from browser the metadata column show one term 'POLICIES', but from code it has multiple duplicates:

POLICIES|93aef6f0-5558-4c55-9fb2-fbe622a59e8c;
POLICIES|93aef6f0-5558-4c55-9fb2-fbe622a59e8c;
POLICIES|93aef6f0-5558-4c55-9fb2-fbe622a59e8c

Apparently, when multiple copies of the same term are pushed by code to a managed metadata column, then it's it is a column validation issue. The SharePoint UI will not complain but the search crawler somehow will have a problem with it and will drop the managed metadata crawling for the all managed columns of that item altogether. The document item itself is with other non-managed columns will be crawled.


On the other hand, I can conclude the default SharePoint metadata controls offer some validation/fix. When editing the document from UI, and just click Save without making any changes, the duplicate terms might get removed.


Solution:
Check you code to see where it tries to update managed metadata fields with duplicate copies of the same term. No exception will be thrown so you have to carefully check your logic.

FYI, This is a related post where merging terms in store lead to the same issue


Question

June 10, 2013

Error: site template requires that the Feature be activated in the site collection

Scenario:
You want to create a site template and use it across your environments such as different site collections. In source Site Collection, you create a site and add content such as web parts, lists..etc. You save that site as a template. In destination Site Collection, you upload that template into Solution Gallery and activate it. Then you try to create a site based on the template, but it fails with the error:

The site template requires that the Feature {FeatureID} be activated in the site collection


The quick solution is go to Site Settings -> Site Collection Features and activate the missing feature then try again. Easy huh?
It depends!.. The problem arise when the missing features are not available in your destination environment such as a 3rd party solution that was installed only in source environment.
An alternative is to abandon this site template, and create a fresh site on the destination site collection to ensure that all features are available before creating the template to be used on the destination. This is only good when the site template is not complex to re-create such as when only small amount of customization had applied to source site. But recreating a full customized site that contains a lot of custom lists and web parts will take long hours or days. Another alternative is to go low-level, fix the template and make it work!


Error Analysis:

Let's first see what's happening. Digging for error details in ULS logs show us this:


SPException thrown: Message: The site template requires that the Feature {eb657559-be37-4b91-a369-1c201183c779} be activated in the site collection.. Stack:    
at Microsoft.SharePoint.Utilities.SPUtility.ThrowSPExceptionWithTraceTag(UInt32 tagId, ULSCat traceCategory, Str
ing resourceId, Object[] resourceArgs)     
at Microsoft.SharePoint.SPWebTemplateElement.VerifyFeatures(XmlNodeList xmlNodeFeatures, SPWe
b applyTemplateToThisWeb, Boolean checkIsFeatureActivatedInSiteCollection)     
at Microsoft.SharePoint.SPWebTemplateElement.VerifyFeaturesInWebTemplate(SPWeb applyTemplateToThisWeb)     
at Microsoft.SharePoint.SPWeb.LoadFeatureWebTemplateContent(SPFeatureWebTemplate featureWebTemplate)     
at Microsoft.SharePoint.SPWeb.ApplyWebTemplate(String strWebTemplate)     
at Microsoft.SharePoint.Solutions.AddGallery.AddGalleryWebPart.CreateSite()
..

Let's have an insider look at the Microsoft.SharePoint.DLL. The following is the code for VerifyFeatures() method in SPWebTemplateElement.cs class file:

private void VerifyFeatures(XmlNodeList xmlNodeFeatures, SPWeb applyTemplateToThisWeb, bool checkIsFeatureActivatedInSiteCollection)
        {
            if ((xmlNodeFeatures != null) && (xmlNodeFeatures.Count != 0))
            {
                foreach (System.Xml.XmlNode node in xmlNodeFeatures)
                {
                    XmlElement element = (XmlElement) node;
                    XmlAttributeCollection attributes = element.Attributes;
                    if (attributes != null)
                    {
                        System.Xml.XmlAttribute attribute = attributes["ID"];
                        if (attribute != null)
                        {
                            Guid guid = new Guid(attribute.Value);
                            SPFeatureDefinition definition = SPFarm.Local.FeatureDefinitions[guid];
                            if (null == definition)
                            {
                                definition = applyTemplateToThisWeb.Site.FeatureDefinitions[guid];
                            }
                            if (null == definition)
                            {
                                SPUtility.ThrowSPExceptionWithTraceTag(0x67323467, ULSCat.msoulscat_WSS_FeaturesInfrastructure, "CannotFindFeatureInstalledSpecifiedInWebTemplate", new object[] { guid.ToString("B") });
                            }
                            if ((checkIsFeatureActivatedInSiteCollection && !applyTemplateToThisWeb.IsRootWeb) && (applyTemplateToThisWeb.Site.Features[guid] == null))
                            {
                                SPUtility.ThrowSPExceptionWithTraceTag(0x67323468, ULSCat.msoulscat_WSS_FeaturesInfrastructure, "CannotFindFeatureActivatedSpecifiedInWebTemplate", new object[] { guid.ToString("B") });
                            }
                        }
                    }
                }
            }
        }


The code reveals that it iterates against all features in the template, and is checks if each feature  is installed then checks if it's activated. The highlighted code in Yellow shows where the error message is being thrown.
In our case we are missing a feature whose ID is: eb657559-be37-4b91-a369-1c201183c779, but  are there other missing features I should worry about? Let's open the template itself.

1. Either import the web template .wsp file using Visual Studio (using the ‘Import SharePoint Solution Package’ project option), or convert the .WSP into .CAB then extract the solution folder.
2. Go to ONet.xml (in Web templates -> Name Template)
3. In <Configurations> section, search inside <SiteFeatures> for missing features in your environment


The Nintex Worklfow solution was installed in my test source Site Collection (but not on destination).
I activated two 3rd party features on Site Collection level. The SiteFeatures section in ONet.xml has the following:
  • "Nintex Workflow 2010" with an ID: 0561d315-d5db-4736-929e-26da142812c5,
  • "Nintex Workflow 2010 Web Parts" has ID: eb657559-be37-4b91-a369-1c201183c779, 
  • and "NintexWorkflowContentTypeUpgrade" with ID: 86c83d16-605d-41b4-bfdd-c75947899ac7
The Nintex Workflow solution is not installed on the destination site collection, and hence these features cannot be installed/activated there.

Note: to know if the missing feature belongs to SP2010 OTB, then check if it exists in this list here.

So here is what's happening:

- the template .wsp file comes with a number of feature IDs that were part of the site collection and site when template created on source environment.
- When the template is installed & activated on destination environment, it tries to reference these features. If one feature does not exist (not installed) or not activated then an SPException error will be thrown.
- As we saw in verifyFeatures() method, the code throws an exception at the first feature missing and abort the site creation but will not give a list of other missing features. This is a drawback in design because it makes it difficult for the Administrator to fix it.


Solution:
Comment out or delete the entries of missing features.

  1. Open the template's ONet.xml to comment or delete every missing feature whether in SiteFeatures or in WebFeatures sections that you cannot activate in your environment, until the resulting template is workable.
  2. Build the project in VS to produce new web template, or repackage the folder into .CAB then save as .WSP file.

Note: If we comment only the first feature, save the template file and upload to Solutions Gallery then create the site then we see a similar error but this time it is regarding a different feature with ID: 86c83d16-605d-41b4-bfdd-c75947899ac7
Which is the next missing feature in line as you can see in the ONet.xml image above.


After you complete the 'cleaning work', your template can now work as charm and the new site was created successfully on destination site collection :)