Using BaseXsltListWebPart and XslListViewWebPart

Working with the . BaseXsltListWebPart sucks, especially when you are trying to use it as a concrete class, extending / changing the way it functions. Everything is sealed or internal, etc. etc. Shock me, shock me with SharePoint. Even if you instantiate an XslListViewWebPart within another WebPart derived class, there are a lot of features that are not available. Most notably, you will lose a bunch of menus.
The best way of doing it is to have a pre-existing BaseXsltListWebPart that already exists. Then you can just loop through the WebParts by using the WebPartManager.WebParts collection, and get a particular WebPart type by using Enumerable.OfType to filter the elements of an IEnumerable based on a specified type, in this case XsltListViewWebPart. Following, we are going to use the XsltListViewWebPart.XmlDefinition property so we can instantiate a new XmlDocument object. After you can execute any changes, then reset the property of the WebPart.

[csharp]

var view = manager.WebParts.OfType().FirstOrDefault();
var xmlDoc = new XmlDocument();
if (view != null) xmlDoc.LoadXml(view.XmlDefinition);
if (xmlDoc.DocumentElement != null)
{
// Do Stuff
}
string xmlDefinition = xmlDoc.OuterXml;
if (view != null) view.XmlDefinition = xmlDefinition;

[/csharp]

Share

SharePoint Taxonomy Error: Terms can not be shared multiple times in the same term set

So let’s assume you have the following situation. You have two term sets. We will call them TermSetXand TermSetY. In TermSetY, you are reusing a TermX from TermSetX. Now let’s assume that you want to reuse a TermY from TermSetX in TermSetY as a child of TermX.

When I was trying to do this I immediately thought the to use the ReuseTerm method (TaxonomyInternalService.ReuseTerm). ReuseTerm is exposed in the TermSetItem concrete class which is used as by the Term and TermSet derived classes. The utility of ReuseTerm is it allows a specification of a source term that is the target for resuse, and return a new Term from the caller TermSet.

If you try to do this quickly you might end up with code like I did, similar to:

[csharp]
if(TermSetY.GetTerm(TermY.Id) == null)
TermX.ReuseTerm(TermY, false);
[/csharp]

You may encounter the error:

Error: Microsoft.SharePoint.Taxonomy.TermStoreOperationException: Terms can not be shared multiple times in the same term set

To solve the error, simply lean on the original ReuseTerm method:

[csharp]
Term termXReference = null;
if (termSetY.GetTerm(termX.Id) == null)
{
termXReference = termSetY.ReuseTerm(termX, false);
}
if (termXReference == null) termSetY.GetTerm(termA.Id);

if(termSetY.GetTerm(termY.Id) == null)
var termYReference = termXReference.ReuseTerm(termY, false);
[/csharp]

Share