0

I have a single document library containing 4 different content types, and I need to be able to make custom edit forms for these content types (it cannot be achieved out of the box.) While I have been able to set the custom edit forms to the content types, I have not been able to successfully redirect to these pages after uploading the file. The upload always defaults to the "Default" content type and then (obviously) re-routes to that content type's edit form. Is there a way to specify a "Source" or something in the URL that would allow me to define which content type we are uploading? The custom edit form DOES NOT contain the "Content Type" choice like the default edit form does, so I am at a bit of a loss.

My initial plan was to store the content type ID in session storage and use it somehow to point to the correct form, but I have yet to succeed with this.

0

If you use SPDesigner, you can add a content typr for each form. For eg: for create form - one CT , for Edit Form - Another CT and so on. Hope this gives you some idea and helps you!

  • SharePoint doesn't really let you make a custom "New" form for document libraries. I can easily make custom Edit forms and assign those to each content type (I have already done this), but I can't seem to figure out how to tell that edit form which content type I just uploaded. Simply clicking "Add Item" in the library sets it to the default content type, and the custom edit form does not let you choose the content type. – Caola May 19 '17 at 14:51
0

You can try updating content type information while creating an item from your custom form.

Get your list of content types for the list and assign proper content type while saving your item.

listitem["ContentType"] = "content type name";
listitem["ContenttypeId"] = "content type id";

You can refer this link for complete example it might work for you.

  • The only problem is I am not creating a custom "new" form (it doesn't let you do that for document libraries), so I am not sure when I would have the opportunity to pass any of that information. – Caola May 19 '17 at 14:40
  • While you are inserting a document into a library how are assigning a content type? – Venkat Konjeti May 19 '17 at 15:16
  • That's the part I need to figure out. If I go to the library itself and click "Add Document," there is no way (that I can see) to determine the content type at that point. If I was using the default edit form, I'd be able to choose a type after upload, but that's not an option on a custom form. – Caola May 19 '17 at 15:27
  • I want to be able to go from a custom page, choose which content type I want from just a simple list on a page, and go from there. I'm just stuck on how to tell the system what content type I'm uploading so I can redirect it accordingly. Or if that's even possible the way I'm doing it... – Caola May 19 '17 at 15:28
  • In SharePoint Out of the box functionality, you have to upload a document to create an item in a library and set metadata to that. It will take default content type. To allow a user to select the content type at the time of uploading a document we can create a custom form using visual studio solution or using a client-side form with JSOM. – Venkat Konjeti May 19 '17 at 15:42
0

If someone could format this for me (and then remove this sentence), I would appreciate it. It's not recognizing my code snippets properly...

Alright, so instead of complicating things with Visual Studio or anything like that, I just went into the XML file for the library itself and found the "ContentType" field name.

Since my whole process is initiated from a custom drop down field on a separate page, I can pass the content type choice from that page into the custom edit form. In this case, I'm not actually using a form; I am using a normal page and simply using SPServices "UpdateListItems." Using sessionStorage, I pass the content type from the initial page to the custom form-like page, and I just make sure to set the appropriate ContentType field to that content type passed when I hit submit.

Here we go from start to finish:

  1. The initial page has a list dropdown formatted nicely to appear as a navigation drop-down may look. The SPServices call then builds the choices based on the content types available:

    $(document).ready(function() {
    $().SPServices({
      operation: "GetListContentTypes", 
      listName: "MyLibrary",
      async: false, 
      completefunc: function (xData, Status) {
        $(xData.responseXML).find("ContentType").each(function() { 
            contentTypeName = $(this).attr("Name");
            contentTypeID = $(this).attr("ID");
            var name = contentTypeName.substr(contentTypeName.indexOf(" ") + 1);
            var type = name.substr(name.lastIndexOf(" ") +1);
            if(name != "Folder" && name != "Document"){
    
                $('#uploadList').append(                    
                        '<li><a href="#" onclick="uploadItem(&#39;'+contentTypeID +'&#39;)">' + name + '</a></li>' 
                );
    
    
            }
        });
      }
    });
    

    });

       function uploadItem(type){
        sessionStorage.setItem('Type', type);
        window.location = '/sitename/sitePages/CustomUpload.aspx';
    
       }
    
  2. Once the user clicks on a choice, they are redirected to the new page that is pretending to be a form. Since I stripped off parts of the name in the previous example, I am redoing the call on the new page. I should probably go back to the initial code and simplify it so I don't have to redo it on the new page, but for now, this works.

    $(document).ready(function(){ type = sessionStorage.getItem('Type'); name = getContentType(type);

    });

    function getContentType(type){ caml = ""+type+""; var contentTypeName = ''; $().SPServices({ operation: "GetListContentTypes", listName: "MyLibrary", async: false, completefunc: function (xData, Status) { $(xData.responseXML).find("ContentType").each(function() { if($(this).attr("ID") == type){ contentTypeName = $(this).attr("Name"); } }); } }); return contentTypeName; }

  3. The page is designed to upload the file and submit the document properties all at the click of a single button. It starts with the document upload portion: A submit button triggers uploadLink()-

    function uploadLink(){      
    var queryStringVals = $().SPServices.SPGetQueryString();
    var uploadID = queryStringVals["ID"];               
    
    saveFileMetadata(uploadID);
    

    } function saveFileMetadata(id){ //pull the data from the custom form var title = $('#title').val(); var author = $('#author').val(); var fp = document.getElementById("FP").checked; //returns true or false var recommended = document.getElementById("recommended").checked; //returns true or false var org = $('#org').val(); var cocom = $('#cocom').val(); var opRel = $('#operationalRelevancy').val(); var region = $('#region').val(); var country = $('#country').val(); var threat = $('#threatActor').val(); var image = $('#imageCat').val(); var pubCat = $('#pubCat').val(); var pubTopic = $('#pubTopic').val(); var AoS = $('#areaOfStudy').val(); var analyticsCat = $('#analyticsCat').val(); var linkType = $('#linkType').val();

    //establish the valuePairs for the UpdateListItems SPServices call
    var valuePairs = [
        ["ContentType", type],
        ["Title", title],
        ["Foreign_x0020_Perspective", fp],
        ["Recommended", recommended],
        ["Organization", org],
        ["COCOM", cocom],
        ["Operational_x0020_Relevancy", opRel],
        ["Region", region],
        ["Country", country],
        ["Threat_x0020_Actor", threat]
    ];
    
    //depending on the content type, push additional values to the valuePairs array         
    if(type == 'Image File'){
        valuePairs.push(["Image_x0020_Category", image]);
    }
    else if(type == 'Publication File'){
        valuePairs.push(["Publication_x0020_Category", pubCat]);
        valuePairs.push(["Publication_x0020_Topic", pubTopic]);
        valuePairs.push(["Product_x0020_Author", author]);
    }
    else if(type == 'Analytics File'){
        valuePairs.push(["Analytics_x0020_Category", analyticsCat]);
        valuePairs.push(["Area_x0020_of_x0020_Study", AoS]);
        valuePairs.push(["Product_x0020_Author", author]);
    }
    else if(type == 'Link'){
        valuePairs.push(["Link_x0020_Type", linkType]);
    }       
    
    //run the UpdateListItems
    $().SPServices({
        operation: "UpdateListItems",
        listName: 'MyLibrary',
        ID: id,
        valuepairs: valuePairs,
        completefunc: function (xData, Status) {
        }
    }); 
    
    //slight pause before redirecting to the original page
    setTimeout(function() { 
        window.location = 'https://mysite/sitename/SitePages/StartPage.aspx';
    }, 
    500);
    
    $("#loadingImg").hide();
    

    }

Now using a completely custom solution, I have allowed users to select the content they are uploading and allowed them to use a customized page that functions as a form.

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.