Not able to query SharePoint from Nintex form custom JavaScript - sharepoint

I'm trying to query the SharePoint list using JSOM from Nintex custom javascript. My intention is to show the data in Nintex multiLine textbox. I can query data from SharePoint and display it in the Nintex edit form and not able to query from the Nintex view/display form.
Any thoughts? Thanks!.
Please refer the below code
var clientContext;
var oListItem;
var workFlowListName = "sharepoint Tasks";
var requestId = 1;
var pollSP;
function checkSPLoad() {
if (clientContext) {
window.clearInterval(pollSP);
if (requestId)
GetPFItems();
}
}
function GetPFItems() {
var oList = clientContext.get_web().get_lists().getByTitle(workFlowListName);
var camlQuery = new SP.CamlQuery();
camlQuery.set_viewXml("<View><Query><Where><Eq><FieldRef Name='ID' /><Value Type='Text'>" + requestId
+ "</Value></Eq></Where></Query></View>");
this.collListItem = oList.getItems(camlQuery);
clientContext.load(collListItem);
clientContext.executeQueryAsync(Function.createDelegate(this, this.PFQuerySucceeded),
Function.createDelegate(this, this.onQueryFailed));
}
function PFQuerySucceeded(sender, args) {
var listItemEnumerator = collListItem.getEnumerator();
while (listItemEnumerator.moveNext()) {
var oListItem = listItemEnumerator.get_current();
pfTaskName = oListItem.get_item('TaskName');
NWF$('#'+'sampleTextBox').val(pfTaskName);
}
}
function onQueryFailed(sender, args) { }

Why your CAML Query has data type as Text for ID field? Also, You need to configure multiline textbox's setting and make display mode as Edit. This should start getting populated.

Related

How to load attachment in client context?

I have a list with attachments. I want to fetch list records with attachment as a url in anchor tag.
For ex- when user selects ID-100 then all its data will be retrieved and attachment in anchor tag to open it.
I used JSOM for this and it is working fine but it is giving error when there is no documents attached.
Please help-
var clientContext = new SP.ClientContext.get_current();
var oList=clientContext.get_web().get_lists().getByTitle(varListName);
var camlQuery = new SP.CamlQuery();
//var camlQuery = "<View><Query><Where><Eq><FieldRef Name='ID' /><Value Type='Integer'>1</Value></Eq></Where></Query></View>";
camlQuery.set_viewXml("<View><Query><Where>" +
"<Eq><FieldRef Name=\"ID\"/><Value Type=\"Number\">" + varid + "</Value></Eq>" +
"</where><OrderBy></OrderBy></Query><RowLimit>1</RowLimit></View>");
this.collListItem = oList.getItems(camlQuery);
var attachmentFolder = clientContext.get_web().getFolderByServerRelativeUrl('Lists/' + varListName + '/Attachments/' + varid);
this.attachmentFiles = attachmentFolder.get_files();
clientContext.load(collListItem);
clientContext.load(attachmentFiles);
clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));
}
We can first check if the item has attachment or not via ListItem.Attachments property.
In case if list List Item contains attachments, submit a second request to retrieve attachment files.
this.collListItem = oList.getItems(camlQuery);
var listItemEnumerator = collListItem.getEnumerator();
// on query success
while (listItemEnumerator.moveNext()) {
var oListItem = listItemEnumerator.get_current();
var hasAttachments = item.get_fieldValues()['Attachments'];
// getAttachmentFiles ....
}
BR

Sharepoint 2013 list views with common buttons menu bar

I have a list with multiple views (around 15 views). How can I add a same set of buttons to all the views below the page title and above list web part? I have SharePoint designer. I have used content editor web part, but I need to add it to all view aspx pages and add the same set of buttons.
Any better solutions?
Thanks
Venky
You could read the existing views with javascript (JSOM) to retrieve all the existing views on the specified list.
In the OnQuerySucceed function while looping through the views you could generate a button and add it the DOM. For example in your content editor add a element and push the buttons in their.
var viewCollection = null;
function runCode() {
var clientContext = new SP.ClientContext.get_current();
if (clientContext != undefined && clientContext != null) {
var web = clientContext.get_web();
var listCollection = web.get_lists();
var list = listCollection.getByTitle("Tasks");
this.viewCollection = list.get_views();
var viewInfo = new SP.ViewCreationInformation();
viewInfo.set_title('MyView');
this.viewCollection.add(viewInfo);
clientContext.load(this.viewCollection);
clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));
}
}
function onQuerySucceeded() {
var viewInfo = 'Tasks list current views: \n\n';
var viewEnumerator = this.viewCollection.getEnumerator();
while (viewEnumerator.moveNext()) {
var view = viewEnumerator.get_current();
viewInfo += view.get_title() + '\n';
}
alert(viewInfo);
}
function onQueryFailed(sender, args) {
alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
}

Extracting information from "Created By" field

Is there any way I could extract the department or email information from the "Created By" field in a custom list?
You should be more specific how you want to extract the information, if it's javascript or c# or via UI.
Here is a simple example, maybe you can do something with it. Let me know if you have any questions.
var ctx = new SP.ClientContext.get_current();
var web = ctx.get_web();
var list = web.get_lists().getByTitle('Kalender');
//this query can be done in other ways, I'm getting all items
var query = SP.CamlQuery.createAllItemsQuery();
var items = list.getItems(query);
ctx.load(items);
ctx.executeQueryAsync(function () {
var itemsEnum = items.getEnumerator();
while (itemsEnum.moveNext()) {
var item = itemsEnum.get_current();
var author = item.get_item('Author');
console.log("Email: " + author.get_email());
console.log("Name: " + author.get_lookupValue());
console.log("ID: " + author.get_lookupId());
}
}, function(sender, args) {
console.log(args.get_message());
});

SP list item - disable "Edit" for all users except for the item creator but still leave a single column editable for all users

Is it possible to use Item-level Permissions in SP 2013 to disable other users modifying items not created by them but still allow them to modify a single field (column) from that item?
I want everyone to be able to input information in that column after clicking "Edit" item button but yet not able to modify any other field if the item is not created by him.
Only the item creator should be able to modify all the fields.
Any ideas how to achieve that are more than welcome :)
You could use client object model for the same as follows:
$(document).ready(function () {
if(CheckCreatedBy() != GetCurrentUser())
{
$("input[Title='EditableByAllUsers']").prop("disabled", true);
}
});
function CheckCreatedBy()
{
var clientContext = new SP.ClientContext.get_current();
var siteColl = clientContext.get_site();
var oList = siteColl.get_rootWeb().get_lists().getByTitle('ChangeEditForm');
var itemId = _spGetQueryParam('id') ;
var camlQuery = new SP.CamlQuery();
camlQuery.set_viewXml('<View><Query><Where><Eq><FieldRef Name=\'ID\'/>' +
'<Value Type=\'Number\'>'+ itemId +'</Value></Eq></Where></Query></View>');
this.collListItem= oList.getItems(camlQuery);
clientContext.load(collListItem);
clientContext.executeQueryAsync(Function.createDelegate(this,this.onCheckCreatedBySuccessMethod), Function.createDelegate(this, this.onCheckCreatedByFailureMethod));
return this.value;
}
function onCheckCreatedBySuccessMethod(sender, args)
{
var CreatedBy = '';
var listItemEnumerator = collListItem.getEnumerator();
while (listItemEnumerator.moveNext()) {
var oListItem = listItemEnumerator.get_current();
CreatedBy = oListItem.get_item('Author').get_lookupValue();
alert(CreatedBy);
return CreatedBy ;
}
}
function onCheckCreatedByFailureMethod(sender, args)
{
alert('request failed ' + args.get_message() + '\n' + args.get_stackTrace());
}
function GetCurrentUser()
{
var ClientContext = new SP.ClientContext.get_current();
this.CurrentWeb = ClientContext.get_web();
ClientContext.load(this.CurrentWeb.get_currentUser());
ClientContext.executeQueryAsync(Function.createDelegate(this, this.onSuccessMethod), Function.createDelegate(this, this.onFailureMethod));
return this.value;
}
function onSuccessMethod(sender, args)
{
var userObject =this.CurrentWeb.get_currentUser().get_title();
return userObject;
}
function onFailureMethod(sender, args) {
alert('request failed ' + args.get_message() + '\n' + args.get_stackTrace());
}
It can also be done using XSL. This will help you with the same.
I hope this helps.

Safely return all lists in site collection that match criteria

I'm using the following code to get all "Announcement" lists in a Web Applications site collection.
Unfortunately, sometimes the current user does not have permission to that site and the page fails with an exception, even inside the try block.
What would be the right way to do the following safely for all users, where even an anonymous user would just get no results?
static public List<SPListMeta> AllSiteAnnouncementsLists()
{
var returnList = new List<SPListMeta>();
foreach (SPSite oSiteCollection in SPContext.Current.Web.Site.WebApplication.Sites)
{
var collWebs = oSiteCollection.AllWebs;
try
{
foreach (SPWeb oWebsite in collWebs)
{
using (oWebsite)
{
var collSiteLists = oWebsite.GetListsOfType(SPBaseType.GenericList);
returnList.AddRange(from SPList oList in collSiteLists where oList.Title == "Announcements" select new SPListMeta(oList));
}
}
}
catch
{
}
}
return returnList;
}
Try give your code the right permission to execute.
using Microsoft.Sharepoint.Administrator;
SPSecurity.RunWithElevatedPrivileges(delegate(){
// Your source code goes here
});
To get all items of the specific list type from the site collection you have to use SPSiteDataQuery. Each user will get only those items they have permissions.
SPWeb web = SPContext.Current.Web;
SPSiteDataQuery query = new SPSiteDataQuery();
//Ask for all lists created from the announcement template.
query.Lists = "<Lists ServerTemplate=\"104\" />";
// Get the Title field. Define here all you need.
query.ViewFields = "<FieldRef Name=\"Title\" />";
// Set the sort order.
query.Query = "<OrderBy>" +
"<FieldRef Name=\"Title\" />" +
"</OrderBy>";
// Query all Web sites in this site collection.
query.Webs = "<Webs Scope=\"SiteCollection\" />";
DataTable dt = web.GetSiteData(query);
DataView dv = new DataView(dt);
This is what ended up working for me, though I do not know if it is the best way to do this.
static public List<SPListMeta> AllSiteAnnouncementsLists()
{
var returnList = new List<SPListMeta>();
var collWebs = SPContext.Current.Web.Site.WebApplication.Sites[0].OpenWeb().GetSubwebsForCurrentUser();
if(SPContext.Current.Site.RootWeb.DoesUserHavePermissions(SPBasePermissions.Open))
{
var collSiteLists = SPContext.Current.Site.RootWeb.GetListsOfType(SPBaseType.GenericList);
returnList.AddRange(from SPList oList in collSiteLists
where oList.DoesUserHavePermissions(SPBasePermissions.ViewListItems)
&& oList.BaseTemplate == SPListTemplateType.Announcements
select new SPListMeta(oList));
}
foreach (SPWeb oWebsite in collWebs)
{
returnList.AddRange(WebRecursion.GetListsForCurrentWeb(oWebsite, SPListTemplateType.Announcements));
foreach (SPWeb oSubSite in oWebsite.Webs)
{
returnList.AddRange(WebRecursion.GetListsForCurrentWeb(oSubSite, SPListTemplateType.Announcements));
}
}
return returnList;
}
public static List<SPListMeta> GetListsForCurrentWeb(SPWeb oWebsite, SPListTemplateType type)
{
var returnList = new List<SPListMeta>();
if (oWebsite.DoesUserHavePermissions(SPBasePermissions.Open))
{
using (oWebsite)
{
var collSiteLists = oWebsite.Lists;
returnList.AddRange(from SPList oList in collSiteLists
where oList.DoesUserHavePermissions(SPBasePermissions.ViewListItems)
&& oList.BaseTemplate == type
select new SPListMeta(oList));
}
}
return returnList;
}

Resources