So, I have asked several questions on this topic and each one resulted in a bit of progress. I believe I am now on the last step and I am not sure why this javascript doesn't work.
The basic goal is to grab a document library's URL based on the parameter of the current page. So for example, if I am on the project dashboard for project 1, the URL would look like this:
oursite.sharepoint.com/sites/subsite/projectPages/projectDashboard.aspx?ProjectCode=Proj1
On this page will be all of the information about the project, including a view of the most recent documents from the associated library(i.e. dl_project1 for this parameter). In order to create a recent documents view, I have an iframe with the source being the document library and a view called recent that shows recently modified items by the current user (i.e. /sites/subsite/dl_project1/forms/recent.aspx)
Now, what I am stuck on is how I retrieve the document library name. I will paste the full javascript i am using below, but the gist of what it does:
- grab the parameter from the current url (ProjectCode=x, returns the x)
- Using a CAML query, find the document library name that is associated with said project code. This is done through a custom list. One column is projectcode, a lookup to the project codes used for parameters. The other column is a text column with the name of the associated doclib. For example i might have: projectCode=proj1 and documentLibrary=dl_proj1
- Break apart the current URL, and reconstruct the correct URL for the document library
Display the doclib using the doclib's url in an iframe
<html> <head> <script type="text/javascript"> ExecuteOrDelayUntilScriptLoaded(retrieveListItems, "sp.js"); function retrieveListItems() { //retrieve the document library's name var parameter = GetUrlKeyValue('ProjectCode'); //get the parameter name, tested and it works var docLibName; var clientContext = new SP.ClientContext(); //create client context at this site collection. var oList = clientContext.get_web().get_lists().getByTitle('ProjectParameters'); //create list object by the title of the, projectParameters is the list to look through var camlQuery = new SP.CamlQuery(); //create CAMLquery to pull in list items. camlQuery.set_viewXml('<View><Query><Where><Eq><FieldRef Name=\'Title\'/>' + '<Value Type=\'Computed\'>' + parameter + '</Value></Eq></Where></Query></View>' + '<RowLimit>10</RowLimit></View>'); //define the query, here we pull in the field where the parameter matches. this.collListitem = oList.getItems(camlQuery);//get the items from the list object based on the query defined above. clientContext.load(collListitem);//telling the object model to prepare to execute the following object by loading it into the client context. clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed)); //break apart the url var pathArray = window.location.pathname.split( '/' ); var firstLevelLocation = pathArray[1]; var secondLevelLocation = pathArray[2]; var thirdLevelLocation = pathArray[3]; var viewUrl = '/Forms/AllItems.aspx'; //put the docliburl back together var docLibUrl = window.location.protocol+ "//" + window.location.host + "/" + firstLevelLocation + "/" + secondLevelLocation + "/" + thirdLevelLocation + "/" + docLibName + viewUrl; //add the docliburl to the iframe's src attribute modIframe(docLibUrl); } function onQuerySucceeded(sender, args) { var listItemInfo = ''; var listItemEnumerator = collListitem.getEnumerator(); while (listItemEnumerator.moveNext()) { var oListItem = listItemEnumerator.get_current(); listItemInfo += oListItem.get_item('i3hc');//ih3c is the internal name for the text column that contains doclib names } docLibName= listItemInfo;//set the doclibname to the pulled field } function onQueryFailed(sender, args) { alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace()); } function modIframe(DocumentLibraryUrl){ document.getElementById('doclibView').src=DocumentLibraryUrl; } </script> </head> <body> <iframe src='' height=400 width=600 id='doclibView'> </iframe> </body> </html>
The problem that currently stands... If i put an alert in the onQuerySucceeded method, to alert the docLibName, it alerts the correct name. However, when i add in the code to display the iframe it doesn't update docLibName for some reason. It says page not found.
Any ideas?