存储sharepoint托管应用程序的属性
Store Properties for a SharePoint-hosted app
我试图找出最好的-或真正的任何工作方式-在SharePoint托管的应用程序中存储键/值对。对需要:
- 在启动时加载,如果设置存在,否则使用默认值。
- 按需创建——即用户可以在UI中添加新设置,然后我在代码的其他地方使用该设置进行更改。例如,使用自定义文本字符串作为列表名称,而不是应用程序的默认设置。
我已经尝试使用PropertyBag,但是在试图写它时得到一个访问拒绝错误。
我也试过使用列表,但在正确使用该技术时遇到了问题。
有谁有好的方法和如何做的建议吗?如果这些是最好的方法,我很乐意重新审视我已经尝试过的技术。
请记住,这个问题应该限制在与sharepoint托管的应用程序一起工作的东西。这意味着c#和服务器端代码不在范围内。
这是我最终使用的解决方案-将设置存储在应用程序的hostweb中的列表中。
它由如下所示的几个函数组成。
CreateSettingsList:
Create创建一个包含Title和Value字段的普通列表,我用它来存储设置名称和与之关联的值。这是在document ready函数中调用的,以确保已经创建了一个列表,如果已经创建了,则继续并尝试从中读取。如果列表之前不存在,我调用一个函数来初始化列表中的默认变量值。
//___________________________________Create settings list________________________________________
function createSettingsList()
{
// Create a SharePoint list with the name that the user specifies.
var hostUrl = decodeURIComponent(getQueryStringParameter("SPHostUrl"));
var hostContext = new SP.AppContextSite(currentContext, hostUrl);
var hostweb = hostContext.get_web();
var listCreationInfo = new SP.ListCreationInformation();
//title the list
listCreationInfo.set_title("PTOAppSettings");
//set the base type of the list
listCreationInfo.set_templateType(100); //generic list
listCreationInfo.set_description("A list for custom app settings. If you have uninstalled the Paid Time Off App with no intention of reinstalling, this list can be safely deleted.");
var lists = hostweb.get_lists();
//use the creation info to create the list
var newList = lists.add(listCreationInfo);
var fieldCollection = newList.get_fields();
//add extra fields (columns) to the list & any other info needed.
fieldCollection.addFieldAsXml('<Field Type="Text" DisplayName="Value" Name="Value" />', true, SP.AddFieldOptions.AddToDefaultContentType);
newList.update();
currentContext.load(fieldCollection);
currentContext.load(newList);
currentContext.executeQueryAsync(onSettingsListCreationSuccess, onSettingsListCreationFail);
}
function onSettingsListCreationSuccess(){
//All is well.
initializeSettings();
}
function onSettingsListCreationFail(sender, args) {
//alert("We didn't create the list. Here's why: " + args.get_message());
//If a list already exists, we expect the creation to fail, and instead try to read from the existing one.
getSetting("VAR_CCEmail");
}
初始化:
Initialize为我将来可能存储的变量创建新的列表项。我将它们的值设置为",如果它们不被使用,则设置为null。
//___________________________________Initialize setting(s)________________________________________
function initializeSettings()
{
//Get info to access host web
var hostUrl = decodeURIComponent(getQueryStringParameter("SPHostUrl"));
var hostContext = new SP.AppContextSite(currentContext, hostUrl);
var hostweb = hostContext.get_web();
//Get list in host web
var lstObject = hostweb.get_lists().getByTitle("PTOAppSettings");
//Prepare an object to add a new list item.
var listItemCreationInfo = new SP.ListItemCreationInformation();
var newItem = lstObject.addItem(listItemCreationInfo);
//Create item. You should repeat this for all the settings you want to track.
newItem.set_item('Title', "VAR_CCEmail");
newItem.set_item('Value', "");
//Write this new item to the list
newItem.update();
currentContext.executeQueryAsync(onListItemSuccess, onListItemFailure);
function onListItemSuccess() {
//Load customizations, if any exist
getSetting("VAR_CCEmail");
}
function onListItemFailure(sender, args) {
bootbox.dialog({
title: "Something went wrong!",
message: "We were unable to initialize the app settings! Here's what we know about the problem: " + args.get_message() + ''n' + args.get_stackTrace(),
buttons: {
success:{
label: "Ok"
}
}
});
}
}
:
Set是一个相当简单的函数,它接受一个设置名称和一个值,并允许您更新存储在给定变量中的值。
//___________________________________Set setting________________________________________
function setSetting(setting, value){
//Get info to access host web
var hostUrl = decodeURIComponent(getQueryStringParameter("SPHostUrl"));
var hostContext = new SP.AppContextSite(currentContext, hostUrl);
var hostweb = hostContext.get_web();
//Get list in host web
var list = hostweb.get_lists().getByTitle("PTOAppSettings");
//A caml query get the appropriate setting
var queryXml = "<View><Query><Where><Eq><FieldRef Name='Title' /><Value Type='Text'>" + setting + "</Value></Eq></Where></Query></View>"
var query = new SP.CamlQuery();
query.set_viewXml(queryXml);
var items = list.getItems(query);
currentContext.load(items);
currentContext.executeQueryAsync(onListItemSuccess, onListItemFailure);
function onListItemSuccess() {
//looking up a specific setting should only return one item in the array.
var item = items.getItemAtIndex(0);
//update the value for the item.
item.set_item("Value", value);
item.update();
}
function onListItemFailure(sender, args) {
bootbox.dialog({
title: "Something went wrong!",
message: "We were unable to set app settings! Here's what we know about the problem: " + args.get_message() + ''n' + args.get_stackTrace(),
buttons: {
success:{
label: "Ok"
}
}
});
}
}
:
Get读取列表,找到您指定的设置,然后确定与该设置关联的Value是"还是null,还是一个实际值。如果它是一个实际值,我将该值写入全局变量,程序使用该全局变量来处理该设置。
//___________________________________Get setting________________________________________
function getSetting(setting) {
var hostUrl = decodeURIComponent(getQueryStringParameter("SPHostUrl"));
var hostContext = new SP.AppContextSite(currentContext, hostUrl);
var hostweb = hostContext.get_web();
var list = hostweb.get_lists().getByTitle("PTOAppSettings");
//A caml query to get manager name for the record where user is equal to current user.
var queryXml = "<View><Query><Where><Eq><FieldRef Name='Title' /><Value Type='Text'>" + setting + "</Value></Eq></Where></Query></View>"
var query = new SP.CamlQuery();
query.set_viewXml(queryXml);
var items = list.getItems(query);
currentContext.load(items);
currentContext.executeQueryAsync(
function() //on success.
{
//get first (and only) item.
var item = items.getItemAtIndex(0);
var value = item.get_item("Value");
//If the property is empty it hasn't been set.
if (value === "" || value === null){
//Return null to the appropriate global variable. Not all of the settings in this switch are implemented in the program yet, but may be later.
switch(setting) {
case "VAR_PaidTimeOff":
paidTimeOffListName = "";
break;
case "VAR_Contacts":
contactsListName = "";
break;
case "VAR_CCEmail":
carbonCopyEmail = "";
break;
}
}
else
{
//Return the value. Not all of the settings in this switch are implemented in the program yet, but may be later.
switch(setting) {
case "VAR_PaidTimeOff":
paidTimeOffListName = value;
break;
case "VAR_Contacts":
contactsListName = value;
break;
case "VAR_CCEmail":
carbonCopyEmail = value;
break;
}
}
},
function(sender,args){
bootbox.dialog({
title: "Something went wrong!",
message: "We were unable to get app settings! Here's what we know about the problem: " + args.get_message() + ''n' + args.get_stackTrace(),
buttons: {
success:{
label: "Ok"
}
}
});
});
}
这可以扩展为包括其他函数来完成其他特殊任务,例如,您可以创建一个"createssetting"函数,该函数允许您动态添加新设置(我在最初的问题中提到的要求之一)。在我的例子中,初始化一组设置满足了我的需要,但其他人可能想要一种方式来写更多。
- 表单's的action属性-如何在javascript中指定完整的应用程序路径
- 使用 SharePoint 查询字符串值作为应用程序部件属性
- jquery.mobile-1.4.5.js:26未捕获类型错误:无法设置属性'移动'react应用程序
- 找不到属性'link_to'on对象(生成的应用程序控制器)
- Webpacked Angular2应用程序类型错误:无法读取属性'getOptional'的未定义
- 角度嵌套对象-TypeError:无法读取属性'应用程序'的未定义
- 使用 Jquery 将属性 Required 添加到 Razor 应用程序中的输入中
- 在 mvc 应用程序中使用 href 属性重定向 asp.net 不起作用
- redux/react 应用程序中的状态有一个带有化简器名称的属性
- 在 Angular 应用程序上收到错误:类型错误:无法读取未定义的属性“then”
- 类型错误:无法读取 nodejs 应用程序中未定义的属性“方法”
- jQuery Mobile Windows应用商店应用程序:JavaScript运行时错误:无法获取属性'ind
- Ti.App.fireEvent不起作用;error未捕获类型错误:无法读取属性'应用程序'的未定义
- 查询嵌入闪亮应用程序中的数据表的状态/属性
- 通过activexobject实现excel应用程序的方法和属性
- 谷歌应用程序脚本中的属性服务出现意外行为
- TypeError:无法读取属性'日期'在Meteor应用程序中使用tsega:bootstrap3日期
- 如何访问angular js应用程序中的javaservlet会话属性
- 在Angularjs应用程序中,使用嵌套对象属性过滤ng repeat不起作用
- Angularjs应用程序中使用AND运算过滤ng个基于重复的多个属性