Hot answers tagged

90

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: ...


34

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;


32

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, ...


24

Looks like nobody really came up with the right answer. There are a lot of answers here but I eventually found what I was looking for. Brage's answer is correct if I had a selection of items to work from. For this to work, you must enable item selection on the view and let the user select more than one item. SP.ListOperation.Selection will give you back a ...


23

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


18

You can use an function as a delegate and then call your function with in. ExecuteOrDelayUntilScriptLoaded(function () { alertThis("Hello World") }, "core.js"); function alertThis(value) { alert(value); } It is worth reading about closures, delegates and Anonymous functions in javascript to help understand the code. SO - How does an anonymous ...


18

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. ...


18

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(); ...


17

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 ...


17

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); ...


16

Yet another version to load List by server relative Url using CSOM. The solution consists of the following steps: Retrieve list Folder object using Web.GetFolderByServerRelativeUrl method Retrieve List by specifying List ID stored in vti_listname property from Folder.Properties object Code: static class WebExtensions { /// <summary> /// ...


15

This seems to be pretty close in performance: context.Load(lists, all => all .Where(l => l.RootFolder.Name == "MyListUrl") .Include(l => l.Id)); context.ExecuteQuery(); list = lists.Single(); If you need to get this by server-relative url you would need to parse the web url and list url first.


15

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 = ...


15

The limit is due to server-driven paging. It protects against developers inadvertently making a request that returns large result sets. You can use $top if you really want a large result set to be returned. Otherwise you can get the results a page at a time by checking __next. If it has a value, it contains the url that will return the next set of items. ...


14

AFAIK, it's only possible to use getByName in SharePoint 2013, not in 2010. See MSDN reference for more information var groupCollection = clientContext.get_web().get_siteGroups(); visitorsGroup = groupCollection.getByName("SomaGroupName");


14

How to activate feature via Managed Client Object Model: public void ActivateFeature(string webFullUrl,Guid featureId,bool force,FeatureDefinitionScope featdefScope) { using (var ctx = new ClientContext(webFullUrl)) { var features = ctx.Web.Features; ctx.Load(features); ctx.ExecuteQuery(); features.Add(featureId, force, ...


14

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 ...


14

You can do it like this: <script> function GetSiteUrl() { var ctx = new SP.ClientContext(); var site = ctx.get_site(); ctx.load(site); ctx.executeQueryAsync(function(s, a){alert(site.get_url())}); } </script> <a href='javascript:GetSiteUrl();'>Get site URL</a> To load only the URL from the site to minimize data ...


14

For those who are still looking for the answer. SharePoint 2013 (since SP1 at least see UPD below) contains special method Web.GetList. It accepts site-relative url for the list, i.e. site/dev/lists/mylist. Sample code for .NET object model: var ctx = new ClientContext("http://sp2013dev/sites/dev"); var list = ...


13

If you can you should use the Client Object Model (CSOM) - it does not support that many features as the web services but are superior in a number of ways such as: data types batching of commands (more efficient usage of bandwith) optimization of data loaded (more efficient usage of bandwidth) more similar to the server side object model in terms of ...


13

You cannot make cross-sitecollection calls from javascript client object model in SharePoint 2010. But you can make such calls in SharePoint 2013: var crossSiteContext = new SP.ClientContext("http://url-of-another-siteCollection/"); var crossSiteWeb = crossSiteContext.get_web(); crossSiteContext.load(crossSiteWeb); ...


12

You load multiple items. A conflict occurs. Collect all items you need and call "load". After you call load for all items, then run context.executeQueryAsync. Save your selected items as a global variable (this.items): function showTitles(urlColumnName) { var context = new SP.ClientContext.get_current(); var web = context.get_web(); var lists = ...


12

i managed to get it working . i changed the scope from "Recursive" to "RecursiveAll", RecursiveAll gets all files and all folders under the specidified location. Caml query was replaced with camlQuery.ViewXml = "<View Scope=\"RecursiveAll\"> " + "<Query>" + "<Where>" + ...


12

Looks like I just needed to add the get_lookupValue() function after my get_item call: alert(oDeptItem.get_item('Department').get_lookupValue()); Hope this helps someone...


11

Retrieving a lookup field is no different than retrieving any other field, but getting at the value takes an extra step. (using your example) var childIdField = oListItem["ChildId"] as FieldLookupValue; if (childIdField != null) { var childId_Value = childIdField.LookupValue; var childId_Id = childIdField.LookupId; } The reason for the extra ...


11

According to How to set people and group column programmatically using Client Object Model this is how you set single user fields: SP.User _newUser = _ctx.Web.EnsureUser("domain\\username"); _ctx.Load(_newUser); _ctx.ExecuteQuery(); SP.FieldUserValue _userValue = new SP.FieldUserValue(); _userValue.LookupId = _newUser.Id; _listItem["userfield"] = ...


10

It is more a caml question than an SPServices question. Often one uses or and and operators to combine multiple values, which, like @rjcp3 said, can be messy. There is another operator, less known, IN operator to choose a range of values (see the xml example below). To simplify the creation of CAML queries in javascript, I'd recommend SharePoint ...


10

Here you go: using (var context = new ClientContext("http://localhost")) { var allProperties = context.Web.AllProperties; allProperties["testing"] = "Hello there"; context.Web.Update(); context.ExecuteQuery(); }


10

Jquery approach can be implemented, but I'm afraid it will be exceedingly complicated, because ribbon buttons can change their size if you resize the browser window (so the id of the button will be changed respectively, for example from ..-Large to ..-Medium, etc.), and also they're created after page load, dynamically with js - so you will need some ...


10

A similar question was asked on StackOverflow some time ago (just found it). This answer provides the information you are searching. To provide a short version of the answer: strictly talking, the line "ULSrE8:;" does nothing (it does not execute the function) BUT the label is used by some intricate diagnostic/error logging code that parses the function ...



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