Creating a Pitchbook – Getting Related Pitchbooks with a Content Query Web Part and Page Query Strings (Part 4)

This is part four of my series of posts on customizing document sets.

You may want to look at these other articles if you haven’t already seen them.

Introducing Document Sets

Creating a Pitchbook - Customizing Document Set Settings (Part 1)

Creating a Pitchbook – Customizing the Document Set Welcome Page (Part 2)

Creating a Pitchbook – Closing the Pitchbook with Document Set Workflow Actions (Part 3)

This time I am going to get into some simple code development by creating a web part that redirects to the welcome page and passes document set properties into the query string. This is done so the content query web part can be used to provide links to related pitchbooks. This is really useful because you can dynamically use document set properties to link to related content, helping users navigate and discover useful content to help them do their work.

Document set welcome pages dynamically load content based on the content of the current document set. This means that certain web parts need some configuration to get the document set properties because they cannot pull them as a property from the current page. When a document set welcome page is accessed, the document set that was clicked on passes its ID as a parameter in the URL. When the page loads it gets the properties of the document set and displays the contents.

The content query web part can be used to filter document library items based on a metadata value of the page the content query web part is on. But since there is only one welcome page for a document set, a value must be dynamically passed in to display different results for each document set. To do this you can use a query string parameter by getting properties from the document set and then redirecting back to the welcome page with the properties in the query string. The content query web can then be configured to filter results based on the value in the query string. 

To handle getting the document set properties and page redirection I created a web part. The web part can then be placed on the document set welcome page. By creating a non-visual web part this can all be handled as a sandboxed solution. In the .cs of the solution I placed the following code:  

 

        //When the page is loaded run the redirection. If the redirection has already occurred you do not want to

        //redirect again so submitted will be set to a value. Only do a redirection if Submitted is not specified

        //in order to prevent redirecting infinitely

        protected override void OnLoad(EventArgs e)

        {

            if (String.IsNullOrEmpty(System.Web.HttpContext.Current.Request.QueryString["Submitted"]))

            {

                if (!String.IsNullOrEmpty(System.Web.HttpContext.Current.Request.QueryString["ID"]))

                {

                    SPSite mySite = SPContext.Current.Site;

                    SPWeb myWeb = SPContext.Current.Web;

                    SPList myList = SPContext.Current.List;

                    Redirect(mySite, myWeb, myList);

                }

            }

        }

 

        //This method gets a managed metadata property from the document set, creates a

        // url, and adds a query string parameter including that property to the URL and

        // redirects back to the same page adding that query string property.

        public void Redirect(SPSite mySite, SPWeb myWeb, SPList myList)

        {

            string thisPage = System.Web.HttpContext.Current.Request.Url.AbsoluteUri;

            string docSetId = System.Web.HttpContext.Current.Request.QueryString["ID"];

            int numVal = Int32.Parse(docSetId);

            SPListItem listItem = myList.GetItemById(numVal);

            SPFolder myFolder = listItem.Folder;

            DocumentSet myItem = Microsoft.Office.DocumentManagement.DocumentSets.DocumentSet.GetDocumentSet(myFolder);

            TaxonomyFieldValue value = listItem["Products"] as TaxonomyFieldValue;

            if (value != null)

            {

                TaxonomySession session = new TaxonomySession(mySite);

                Guid termGuid = new Guid(value.TermGuid);

                Term term = session.GetTerm(termGuid);

                thisPage += "&Products=" + term.Name;

            }

            thisPage += "&DocumentID=";

            SPField documentId = myItem.Item.Fields["Document ID Value"];

            thisPage += documentId.GetFieldValueAsText(myDocSet.Item["Document ID Value"]);

 

            thisPage += "&Submitted=yes";

            System.Web.HttpContext.Current.Response.Redirect(thisPage);

        }

If submitted has been specified and there is an ID in the query string, the Redirect method is fired. When this occurs the url will be obtained. The properties are then collected from the current document set. In this example I get the value for the managed metadata column Products. I also get the property for the Document ID Value. This column is on the document set content type when the Document ID site collection feature is activated. This assigns a unique identifier that will be used to filter results so the current document set is not displayed.

Now I test the redirection and make sure that when I have a value specified for the Products column, the appropriate value shows up in the query string. Now I add a content query web part to the document set welcome page. I specify the document library where the pitchbooks are located and select the Pitchbook content type. In the additional filters section of the content query web part I select Products and enter [PageQueryString: Products]. I also specify the Document ID Value column is not equal to [PageQueryString: DocumentID], otherwise the current document set will appear in the results. I save the content query web part properties and now this will return pitchbooks that have the same products property that the current document set has. The number of results can be limited.

Now when I access a pitchbook, I will get results of other pitchbooks that have the same products. One thing to remember is that Products is a managed metadata column, which can have multiple values with the same label. Since this is just filtering on the label, it won’t return the results you would expect if there are multiple terms with the same label.

In this article I showed how to get related document sets and display the results on the document set welcome page using page query strings and the content query web part. In the next post of this series I will be joined by Carlos, a developer at Microsoft, and we will demonstrate customizing the document set ribbon and using the document set export object model.

Quentin Christensen

Program Manager