133

Looks like you're looking for MSOLayout_InDesignMode var inDesignMode = document.forms[MSOWebPartPageFormName].MSOLayout_InDesignMode.value; if (inDesignMode == "1") { // page is in edit mode } else { // page is in browse mode } This will refer to value of the following html input control, which is rendering on the page when it is in edit mode: &...


46

You can do it without any SharePoint calls by using the default JavaScript location object (W3schools) and the page variable _spPageContextInfo (Ted Pattison's Blog). Something like: var url = window.location.protocol + "//" + window.location.host + _spPageContextInfo.siteServerRelativeUrl;


38

SP.Ribbon.PageState.Handlers.isInEditMode() Returns true or false. I use this in publishing pages.


38

How to check if current user belongs to SharePoint group via CSOM (JavaScript): function IsCurrentUserMemberOfGroup(groupName, OnComplete) { var currentContext = new SP.ClientContext.get_current(); var currentWeb = currentContext.get_web(); var currentUser = currentContext.get_web().get_currentUser(); currentContext.load(...


37

These techniques are silly. On every SharePoint page there's a javascript context variable: _spPageContextInfo { webServerRelativeUrl : "/ProjectWeb", webLanguage : 1033, currentLanguage : 1033, webUIVersion : 4, pageListId : "{c1d7b89f-f07b-4e2e-b89c-76c315831d59}", pageItemId : 5, userId : 68, alertsEnabled : true, ...


36

May not be the best wat but it works: var test = "<div id='correspondence'>" + result + "</div>"; $('body').append(test); SP.UI.ModalDialog.showModalDialog({ html: document.getElementById('correspondence'), title: "Correspondence", allowMaximize: false, showClose: true, autoSize: true });


35

In SharePoint 2013 On Premise we can send emails using REST API where we can utilise SP.Utilities.Utility.SendEmail for doing the job. Note: The recipient is limited to a valid SharePoint user for security reasons. function processSendEmails() { var from = 'asad@Example.com', to = 'someone@Example.com', body = 'Hello World Body', ...


33

Juergen, The below article should help: http://www.collabware.com/blog/2013/03/22/tips-tricks-sharepoint-2013-modal-dialogs You have SP.UI.ModalDialog.showWaitScreenWithNoClose which can be used.. function RequestEnded(sender, args) { try { waitDialog.close(); waitDialog = null; } catch (ex) { } }; function RequestStarted(sender, args) { ...


33

Since your "today" variable is a JavaScript date object, you can get the string representation by calling the toISOString() method. ...$filter=StartDate ge datetime'" + today.toISOString() + "' and ...


32

I have used SharePoint’s JavaScript Object Model to retrieve the current logged in username.Once you have executed the load and executeQueryAsync methods and the call to SharePoint was sucessful, then inside of the onQuerySucceeded method is where I display the username to the browser using the SP.Principal.get_loginName property. Try it out…you can host ...


30

You are using ExecuteOrDelayUntilScriptLoaded wrong, you should use the function name, not execute it. Like this: jQuery(document).ready(function() { ExecuteOrDelayUntilScriptLoaded(myfunction, "sp.js"); }); Note that i removed the "()" from myfunction. Now it should work as expected


29

SharePoint adds a String.format function so you can use: String.format('{0:yyyy}-{0:MM}-{0:dd}',new Date('2015-10-30T05:00:00Z')); Note: String.format is defined in msajaxbundle.js, loaded even before most of the JavaScript files so safe to use without SOD requirements or anything. It was modelled after the C# and VB implementations, so the MSDN ...


28

It extracts one named parameter from the QueryString (URL) http://domainname.com/page.aspx?parameter1=xxx&parameter2=yyy var value=getQueryStringParameter('parameter1'); It is used in many examples and most of the time not necessary (and bloats your code) because SharePoint has multiple implementations in core libraries Usage depends on whether you ...


27

This code is the basic implementation of LoadSodByKey, this is the best implementation of the function as it has the override to allow you to have a callback for when the script is loaded. After jquery is loaded, the anonymous function will write "Hello World" to the console <script type="text/javascript"> LoadSodByKey("js/jquery-1.9.0.min.js", ...


27

If you're dealing with a collection of items (example all of the items in a list, or all of the lists in a web), you need to use: clientContext.load(collOfItems, 'Include(Field1,Field2)'); For single items (like an individual site, list, or item) you need to use a comma seperated list of string arguments after the item to load, like: clientContext.load(...


26

It depends whether User/Lookup field value is multiple or not, the following formats are used when specifying field value via SharePoint REST: Single User/Lookup value format: FieldName: LookupIdVal Mutiple User/Lookup value format: FieldName: {"results": [LookupIdVal1,LookupIdVal2] } How to set multiple lookup/user field value via SharePoint REST API var ...


26

In this situation I usually disable click event on default form save buttons (as long as it is sharepoint default form) and call my async functions instead. When the job is done, I call save function myself. It works well. Example: var originalSaveButtonClickHandler = function(){}; $(document).ready( function () { var saveButton = $("[name$='...


25

There is also a global object available called 'PageState'. Among the various members of state information is "ViewModeIsEdit". If the value of this member is "1" then you are in edit mode (e.g. PageState.ViewModeIsEdit === "1"). Note: This variable is initialized after sp.ribbon.js gets loaded and is initialized from the _spBodyOnLoadFunctionNames array ...


25

The Load and ExecuteQuery combo (or ExecuteQueryAsync) is a necessary evil of any client-server programming. Your data resides in a remote location (your SharePoint server), so you cannot directly access it. In fact this is true even of any server-side programming, it is only less obvious. When you call Load, you are only preparing your query. As this ...


24

You can get this from SP.FieldChoice.get_choices() (SPFieldChoice inherits SPFieldMultiChoice) To get that object you need to use context.castTo to cast SP.Field to SP.FieldChoice. // Setup context and load current web var context = new SP.ClientContext.get_current(); var web = context.get_web(); context.load(); // Get task list var taskList = web....


24

Here is my fix to the problem. It seems in SharePoint 2013 calling ExecuteOrDelayUntilScriptLoaded() doesn't work on published pages. This is the correct way to do it according to Microsoft (see: http://msdn.microsoft.com/en-us/library/jj245759.aspx) // Make sure the SharePoint script file 'sp.js' is loaded before your code runs. SP.SOD.executeFunc('sp.js'...


22

As James mentioned, your main mistake is that you are using Page.Header.Controls. It is much simplier: this.Controls works like a charm. If it will not work, you should then check your delegate control declaration XML code. It should look like this: <Control Id="AdditionalPageHead" Sequence="1000" ControlSrc="~/_controltemplates/MyCompany/...


21

Here's a better way than the code from John by using plain JavaScript (no need for jQuery) and without adding the element to the body manually: var element = document.createElement('div'); element.innerHTML = 'Hello World, I am the dialog content'; SP.UI.ModalDialog.showModalDialog({ html: element, title: 'My Dialog Title', allowMaximize: false,...


20

This could be done, but unfortunately only using two requests: first to retrieve view query text, second - to retrieve the items. function getItemsFromView(listTitle, viewTitle) { var context = new SP.ClientContext.get_current(); var list = context.get_web().get_lists().getByTitle(listTitle); var view = list.get_views().getByTitle(viewTitle); ...


20

The SharePoint Client Side Object Model works a bit different as on the server side. Before you can use an object you must first load it and then you must execute a query to retrieve the object. In this blogpost you can find a description how to use it. $(document).ready(function () { ExecuteOrDelayUntilScriptLoaded(loadConstants, "sp.js"); }); function ...


20

This blog has a code snippet that is useful: // CREATE Operation // listName: The name of the list you want to get items from // weburl: The url of the web that the list is in. // newItemTitle: New Item title. // success: The function to execute if the call is sucesfull // failure: The function to execute if the call fails function ...


20

This error occurs since tag variable is not defined. About eTag When updating or deleting items within SharePoint lists via REST you must specify the Entity Tag (eTag) value that was returned with the item during the initial query. This enables SharePoint to determine if the item has changed since it was requested. Alternatively you can tell ...


19

If you are working on SharePoint Online, then they have introduced a new variable to hold the current user's login name in the _spPageContextInfo global object. _spPageContextInfo.userLoginName; This will get you the login name without making any AJAX calls with JSOM or REST.


19

You can use the Client Object Model for this: var clientContext; // Make sure the SharePoint script file 'sp.js' is loaded before your code runs. SP.SOD.executeFunc('sp.js', 'SP.ClientContext', sharePointReady); // Create an instance of the current context. function sharePointReady() { clientContext = SP.ClientContext.get_current(); clientContext....


19

here is a better solution. Enjoy and let me know if you need more help :) ExecuteOrDelayUntilScriptLoaded(function() { g_uploadType = DragDropMode.NOTSUPPORTED; SPDragDropManager.DragDropMode = DragDropMode.NOTSUPPORTED; }, "DragDrop.js");


Only top voted, non community-wiki answers of a minimum length are eligible