var context;
var hostweburl;
var appweburl;
var appContextSite;
var list;
var web;
var collListItem;
var user;
$(document).ready(function () {
SP.SOD.executeFunc('sp.js', 'SP.ClientContext',
getUrl);
});
function getUrl () {
hostweburl = getQueryStringParameter("SPHostUrl");
appweburl = getQueryStringParameter("SPAppWebUrl");
hostweburl =
decodeURIComponent(hostweburl);
appweburl =
decodeURIComponent(appweburl);
var scriptbase = hostweburl + "/_layouts/15/";
$.getScript(scriptbase + "SP.Runtime.js",
function () {
$.getScript(scriptbase
+ "SP.js",
function () {
$.getScript(scriptbase + "SP.RequestExecutor.js", execOperation); }
);
}
);
}
function getQueryStringParameter(paramToRetrieve) {
var params =
document.URL.split("?")[1].split("&");
for (var i = 0; i < params.length; i = i + 1) {
var singleParam =
params[i].split("=");
if (singleParam[0] ==
paramToRetrieve)
return singleParam[1];
}
}
var collGroup;
var collUser;
var oList;
var user;
var oList1;
function execOperation() {
context = new SP.ClientContext(appweburl);
var factory =
new SP.ProxyWebRequestExecutorFactory(
appweburl
);
context.set_webRequestExecutorFactory(factory);
appContextSite
= new
SP.AppContextSite(context, hostweburl);
web
= appContextSite.get_web();
context.load(web);
user
= appContextSite.get_web().get_currentUser();
context.load(user);
collGroup
= appContextSite.get_web().get_siteGroups();
context.load(collGroup,
'Include(Title,Id,Users.Include(Title,LoginName))');
context.executeQueryAsync(onSuccess,
onFail);
}
function onSuccess() {
var userInfo = '';
var groupEnumerator = collGroup.getEnumerator();
while (groupEnumerator.moveNext()) {
var oGroup = groupEnumerator.get_current();
if (oGroup.get_title() == 'jaloop
Owners') {
collUser = oGroup.get_users();
var userEnumerator = collUser.getEnumerator();
while (userEnumerator.moveNext()) {
var oUser = userEnumerator.get_current();
this.userInfo += '\nGroup
ID: ' + oGroup.get_id() +
'\nGroup Title: ' +
oGroup.get_title() +
'\nUser: ' + oUser.get_title()
+
'\nLogin Name: ' +
oUser.get_loginName();
oList =
appContextSite.get_web().get_lists().getByTitle('FlagUsers');
var itemCreateInfo = new SP.ListItemCreationInformation();
var oListItem = oList.addItem(itemCreateInfo);
oListItem.set_item('Title',
user.get_email());
oListItem.set_item('ManagerEmail',
oUser.get_loginName());
oListItem.set_item('Subject', 'Subject');
oListItem.set_item('PageName', web.get_url()+'/Pages/Default1.aspx');
oListItem.update();
context.load(oListItem);
var listCreationInfo = new SP.ListCreationInformation();
listCreationInfo.set_title('CustomList'); // list name
listCreationInfo.set_description('description'); // list description
listCreationInfo.set_templateType(SP.ListTemplateType.genericList); //list type
web =
appContextSite.get_web();
var list1 = web.get_lists().add(listCreationInfo);
context.load(list1);
fields = list1.get_fields();
fields.addFieldAsXml(
"<Field Type='Text'
DisplayName='ManagerEmail'/>",
true,
SP.AddFieldOptions.addToDefaultContentType);
fields.addFieldAsXml(
"<Field Type='Text'
DisplayName='Subject'/>",
true,
SP.AddFieldOptions.addToDefaultContentType);
fields.addFieldAsXml(
"<Field Type='Text'
DisplayName='PageName'/>",
true,
SP.AddFieldOptions.addToDefaultContentType);
context.load(fields);
context.executeQueryAsync(onGetUserNameSucces, onGetUserNameFail);
}
}
}
}
function onFail(sender, args) {
alert( args.get_message());
}
function onGetUserNameSucces() {
alert("insert");
}
function onGetUserNameFail(sender, args) {
// TODO:
implement your own fail handler and do some logging
}
//---------------------------------------------------------------------------------------------
No comments:
Post a Comment