2

I have a Document Requests list, which is a list of requests to create a new document or to change an existing document. A user completes a form in which they specify what their document request is for: Change Existing Document RequestIn the case of a request to change an existing document, the user form is to show a list of documents available for changing, which is based on the selected Document Type: Document Types List - the Document Type column is a normal Choice column.

The documents are from different document libraries within the same site collection that relate to the Document Types. For example, the Document Type "Manual/Policy" is related to the Manuals and Policy Documents document library, the Document Type "Procedure" is related to the Procedure Documents document library, and so on.

So I think what would need to happen is the form would need to do a search for documents based on the selected Document Type and then display these documents in a Choice column.

The problem I'm having is, how would I go about implementing this? I have looked at this thread - Is it possible to create a Link to a Document with REST or JSOM? but was not sure if this would work in forms built in SharePoint Designer 2013 (what I am using.)

I must add that my custom SharePoint dev knowledge is rather limited. If there are threads/links that can help out, I'd appreciate being pointed in their direction.

EDIT: I have tried using jQuery and the REST API to call the documents, but am getting a "'__spPageContextInfo' is undefined" error when the form loads. I have checked the Network tab in Dev tools, and all the SharePoint js files are loading correctly (they are loaded by the Master Page). Here's the code I'm using, which is under the tag on the form:

<script type="text/javascript">
_spBodyOnLoadFunctionNames.push("dropdownvalue");

var requestUri = __spPageContextInfo.webAbsoluteUrl;
var requestHeaders = {
"accept": "application/json;odata=verbose"
}       

function getTagFromIdentifierAndTitle(tagName, identifier, title) 
{
    var len = identifier.length;
    var tags = document.getElementsByTagName(tagName);
    for (var i=0; i < tags.length; i++) 
    {
        var tempString = tags[i].id;
        if (tags[i].title == title && (identifier == "" || tempString.indexOf(identifier) == tempString.length - len)) 
        {
            return tags[i];
        }
    }
return null;
}

function dropdownvalue() 
{
    //add an onchange event to the Request To dropdown
    getTagFromIdentifierAndTitle("select","DropDownChoice","Request To").onchange = function() {ChangeEvent()};
    //add an onchange event to the Document Type dropdown
    getTagFromIdentifierAndTitle("select","DropDownChoice","Document Type").onchange = function() {ChangeEvent()};

}

function ChangeEvent()
{
    //get the Request To and Document Type dropdowns
    var dropDownRequestTo = getTagFromIdentifierAndTitle("select","DropDownChoice","Request To");
    var dropDownDocType = getTagFromIdentifierAndTitle("select","DropDownChoice","Document Type");
    //get the selected value
    var requestToOption = dropDownRequestTo.options[dropDownRequestTo.selectedIndex].text;
    var docTypeOption = dropDownDocType.options[dropDownDocType.selectedIndex].text;

    if (requestToOption == "Change Existing Document")
    {
        jQuery("#changeExistDocList").show();
        jQuery("#newDocumentFormat").hide();

        if (docTypeOption == "Manual/Policy")
        {
            //Sphe Notes: Code to search Manuals and Policy Documents Document library here
            requestUri = requestUri + "/_api/web/lists/getByTitle('Manuals and Policy Documents')/items?$select=EncodedAbsUrl"; 
        }
        if (docTypeOption == "Procedure")
        {
            //Sphe Notes: Code to search Procedure Document library here
        }
        if (docTypeOption == "Flow Diagram")
        {
            //Sphe Notes: Code to search Flow Diagram Document library here
        }
        if (docTypeOption == "Supporting Documents")
        {
            //Sphe Notes: Code to search Supporting Documents Document library here
        }
        if (docTypeOption == "Service Level Agreement")
        {
            //Sphe Notes: Code to search Service Level Agreement Document library here
        }
    }
    if (requestToOption== "Create New Document")
    {
        jQuery("#changeExistDocList").hide();
        jQuery("#newDocumentFormat").show();
    }

    $.ajax({
        url: requestUri,
        type: 'GET',
        dataType: 'json',
        headers: requestHeaders,
        success: function (data) 
        {        
           $.each(data.d.results, function(i,result) {
                var path = result.EncodedAbsUrl;
             });

        },
        error: function ajaxError(response) {
            alert(response.status + ' ' + response.statusText);
        }
    });

}

  • 2
    I'd suggest to explore the tag cascading – moe Jun 13 '17 at 8:58
  • Thank you moe. I am looking into it now and will report my findings – user67823 Jun 14 '17 at 8:02
  • @moe I've been researching SPServices, and from what I can gather, the columns would need to be looking at the same list. In my example, the Document Type is in relation to respective document libraries i.e. there's a Manual/Policy document library, a Procedure document library, etc. Not sure how I'd implement it in this case, but I'll keep digging... – user67823 Jun 14 '17 at 9:24
  • I feel like that's relevant information that you should edit in to the original question to more precisely scope it. – moe Jun 14 '17 at 9:28
  • You can do it using jquery and spservices. You can inject your jquery,javascript code using content editor webpart into your form (add/edit). – Dhaval Kariya Jun 14 '17 at 13:49
0

I managed to figure out a solution. I modified the requestUri variable to take in the pageContext (defined as _spPageContextInfo.webAbsoluteUrl) together with a REST API call to each respective Document Library, retrieving the items in each library by their title:

<script type="text/javascript">
_spBodyOnLoadFunctionNames.push("dropdownvalue");

var pageContext = _spPageContextInfo.webAbsoluteUrl;
var requestUri;

var dropDownRequestTo;
var dropDownDocType;
var dropDownSelectDocument;
var requestToOption;
var docTypeOption;

var requestHeaders = {
"accept": "application/json;odata=verbose"
}       

function getTagFromIdentifierAndTitle(tagName, identifier, title) 
{
    var len = identifier.length;
    var tags = document.getElementsByTagName(tagName);
    for (var i=0; i < tags.length; i++) 
    {
        var tempString = tags[i].id;
        if (tags[i].title == title && (identifier == "" || tempString.indexOf(identifier) == tempString.length - len)) 
        {
            return tags[i];
        }
    }
return null;
}

function dropdownvalue() 
{
    //add an onchange event to the Request To dropdown
    getTagFromIdentifierAndTitle("select","DropDownChoice","Request To").onchange = function() {ChangeEvent()};

    //add an onchange event to the Document Type dropdown
    getTagFromIdentifierAndTitle("select","DropDownChoice","Document Type").onchange = function() {DocTypeChangeEvent()};

}   

function ChangeEvent()
{
    //get the Request To and Document Type dropdowns
    dropDownRequestTo = getTagFromIdentifierAndTitle("select","DropDownChoice","Request To");
    dropDownDocType = getTagFromIdentifierAndTitle("select","DropDownChoice","Document Type");
    dropDownSelectDocument = getTagFromIdentifierAndTitle("select","DropDownChoice","Select Document");

    //get the selected value from the Request To dropdown
    requestToOption = dropDownRequestTo.options[dropDownRequestTo.selectedIndex].text;
    //dropDownSelectDocument.remove();
    console.log("ChangeEvent has been called");

    if (requestToOption == "Change Existing Document")
    {
        jQuery("#selectExistDocList").show();
        jQuery("#newDocumentFormat").hide();
        DocTypeChangeEvent();           
    }
    if (requestToOption == "Create New Document")
    {
        jQuery("#selectExistDocList").hide();
        jQuery("#newDocumentFormat").show();
    }           
}

function DocTypeChangeEvent()
{
    //get the selected value from the Document Type dropdown
    docTypeOption = dropDownDocType.options[dropDownDocType.selectedIndex].text;

    if (docTypeOption == "Manual/Policy")
    {
        requestUri = pageContext + "/_api/web/lists(guid'1D655029-12B3-43DA-992C-AC366C2951D9')/items?select=Title";
    }
    if (docTypeOption == "Procedure")
    {
        requestUri = pageContext + "/_api/web/lists(guid'231BD1F6-C8FC-4F34-B7EE-3555D40BA3D4')/items?select=Title";
    }
    if (docTypeOption == "Flow Diagram")
    {
        requestUri = pageContext + "/_api/web/lists(guid'10EC5FEB-AB0B-4C36-8E2F-D31C0AD97741')/items?select=Title";
    }
    if (docTypeOption == "Supporting Documents")
    {
        requestUri = pageContext + "/_api/web/lists(guid'3F13D3E0-8008-455C-9A2F-C8D45DD62D6A')/items?select=Title";
    }
    if (docTypeOption == "Service Level Agreement")
    {
        requestUri = pageContext + "/_api/web/lists(guid'A0D5373D-83F8-4CCC-A418-64180AE42432')/items?select=Title";
    }

    jQuery.ajax({
    url: requestUri,
    method: 'GET',
    dataType: 'json',
    headers: requestHeaders,
    success: function (data) 
    {
        var resultsList = '';
        console.log("number of items in dropDownSelectDocument: " + dropDownSelectDocument.length);

        for (var i=1;i<dropDownSelectDocument.length;i++)
        {
            dropDownSelectDocument.remove(i);
        }

        for (var j=0;j<data.d.results.length;j++)
        {
            console.log(data.d.results[j].Title);
            resultsList = document.createElement("option");
            resultsList.text = data.d.results[j].Title;
            dropDownSelectDocument.add(resultsList);     
        }                     
    },
    error: function ajaxError(response) {
        alert(response.status + ' ' + response.statusText);
    }
    });
}

Thanks everybody!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.