none
Javascript not Working RRS feed

  • Question

  • Hi everyone,

    i have this script and it works if i use it in the chrome development console on my SharePoint page.

    But if i paste the script into a content editor or script editor on my SharePoint page it doesnt work.

    <script type="text/javascript">
    var viewId = '{C4CBD99A-B49F-4D97-8C1D-7FC6B64884A6}';
    var orderFieldValue = 7; // ID of the master item
    var orderFieldInternalName = "shift"; // Internal name of the lookup field
    var jsGridContainer = $get("spgridcontainer_" + g_SPGridInitInfo[viewId].jsInitObj.qualifier)
    var jsGrid = jsGridContainer.jsgrid;
    var lock = 0;
    jsGrid.AttachEvent(SP.JsGrid.EventType.OnEntryRecordPropertyChanged, function(args) {
        if (lock == 0) {
            lock = 1;
            var update = SP.JsGrid.CreateUnvalidatedPropertyUpdate(args.recordKey,orderFieldInternalName,orderFieldValue,false);
            jsGrid.UpdateProperties([update], SP.JsGrid.UserAction.UserEdit);
            lock = 0;
        }
    });
    </script>

    Thanks.

    BR 

    Atilla

    Monday, April 3, 2017 2:52 PM

Answers

  • Hi Atilla,

    Please try to add the code below into a script editor web part in your list view page.

    <script src="//code.jquery.com/jquery-3.1.0.min.js" type="text/javascript"></script>
    <script type="text/javascript">
    $(function () {
    	var viewId = '{C4CBD99A-B49F-4D97-8C1D-7FC6B64884A6}';
    	var orderFieldValue = 7; // ID of the master item
    	var orderFieldInternalName = "shift"; // Internal name of the lookup field
    	var jsGridContainer = $get("spgridcontainer_" + g_SPGridInitInfo[viewId].jsInitObj.qualifier)
    	var jsGrid = jsGridContainer.jsgrid;
    	var lock = 0;
    	jsGrid.AttachEvent(SP.JsGrid.EventType.OnEntryRecordPropertyChanged, function(args) {
    		if (lock == 0) {
    			lock = 1;
    			var update = SP.JsGrid.CreateUnvalidatedPropertyUpdate(args.recordKey,orderFieldInternalName,orderFieldValue,false);
    			jsGrid.UpdateProperties([update], SP.JsGrid.UserAction.UserEdit);
    			lock = 0;
    		}
    	});
    });
    </script>

    Or check if the Minimal Download Strategy (MDS) feature enabled.

    https://msdn.microsoft.com/en-us/library/office/dn456544.aspx

    If you want to achieve some requirement using JavaScript in Quick Edit, sometime we can also use JSLINK to achieve it.

    https://chuvash.eu/2014/11/28/disabling-a-column-in-quick-edit/

    You can also provide more information about your requirement further research.

    Best Regards,

    Dennis


    Please remember to mark the replies as answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com

    • Marked as answer by Atilla87 Tuesday, April 4, 2017 3:12 PM
    Tuesday, April 4, 2017 7:45 AM
    Moderator
  • Hi Atilla87,

    It does not work with URL containing "_layouts/15/start.aspx#" because of Minimal Download Strategy (MDS) feature enabled. You can disable it or go with JSLink option as suggested by Dennis.

    Thanks!


    • Edited by Viral B Patel Tuesday, April 4, 2017 4:18 PM
    • Marked as answer by Atilla87 Wednesday, April 5, 2017 7:43 AM
    Tuesday, April 4, 2017 4:17 PM

All replies

  • Hi Atilla

    Try the following steps

    1. Save the code in a js file

    2. Upload the js file to "Site Assets" library

    3. Use content editor webpart to reference the js. file from "Site Assets" library.

    Good luck

    Reshmee

    Monday, April 3, 2017 3:26 PM
  • Hi Reshmee,

    this doesnt work too.

    BR

    Atilla

    Tuesday, April 4, 2017 7:27 AM
  • Hi Atilla,

    Please try to add the code below into a script editor web part in your list view page.

    <script src="//code.jquery.com/jquery-3.1.0.min.js" type="text/javascript"></script>
    <script type="text/javascript">
    $(function () {
    	var viewId = '{C4CBD99A-B49F-4D97-8C1D-7FC6B64884A6}';
    	var orderFieldValue = 7; // ID of the master item
    	var orderFieldInternalName = "shift"; // Internal name of the lookup field
    	var jsGridContainer = $get("spgridcontainer_" + g_SPGridInitInfo[viewId].jsInitObj.qualifier)
    	var jsGrid = jsGridContainer.jsgrid;
    	var lock = 0;
    	jsGrid.AttachEvent(SP.JsGrid.EventType.OnEntryRecordPropertyChanged, function(args) {
    		if (lock == 0) {
    			lock = 1;
    			var update = SP.JsGrid.CreateUnvalidatedPropertyUpdate(args.recordKey,orderFieldInternalName,orderFieldValue,false);
    			jsGrid.UpdateProperties([update], SP.JsGrid.UserAction.UserEdit);
    			lock = 0;
    		}
    	});
    });
    </script>

    Or check if the Minimal Download Strategy (MDS) feature enabled.

    https://msdn.microsoft.com/en-us/library/office/dn456544.aspx

    If you want to achieve some requirement using JavaScript in Quick Edit, sometime we can also use JSLINK to achieve it.

    https://chuvash.eu/2014/11/28/disabling-a-column-in-quick-edit/

    You can also provide more information about your requirement further research.

    Best Regards,

    Dennis


    Please remember to mark the replies as answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com

    • Marked as answer by Atilla87 Tuesday, April 4, 2017 3:12 PM
    Tuesday, April 4, 2017 7:45 AM
    Moderator
  • Hi Dennis,

    the javascript code does work. But i have one big problem. The javascript code just works if i open the SharePoint Page like this:
    sitecollectionurl/sites/example/SitePages/example.aspx

    But if i open the page over the left menu bar, SharePoint opens the page with following link in the address bar, although i created the link with the url above:
    sitecollectionurl/sites/example/_layouts/15/start.aspx#/SitePages/example.aspx

    BR

    Atilla

    Tuesday, April 4, 2017 3:20 PM
  • Hi Atilla87,

    It does not work with URL containing "_layouts/15/start.aspx#" because of Minimal Download Strategy (MDS) feature enabled. You can disable it or go with JSLink option as suggested by Dennis.

    Thanks!


    • Edited by Viral B Patel Tuesday, April 4, 2017 4:18 PM
    • Marked as answer by Atilla87 Wednesday, April 5, 2017 7:43 AM
    Tuesday, April 4, 2017 4:17 PM