[英]How to call an external restful using $.ajax from within a SharePoint-hosted app (SharePoint Online)
[英]Store Properties for a SharePoint-hosted app
我試圖在SharePoint托管的應用程序中存儲關鍵/值對的最佳 - 或者實際上是任何工作方式。 這對需要:
我嘗試使用PropertyBag,但在嘗試寫入時遇到Access Denied錯誤。
我也嘗試使用列表,但是在使該技術正常工作時遇到了問題。
有沒有人有一個好方法的建議以及如何做。 我很樂意重新審視我已經嘗試過的技術,如果這些是最好的方法。
請記住,此問題應僅限於與SharePoint托管的應用程序一起使用的內容。 這意味着C#和服務器端代碼都沒有了。
這是我最終使用的解決方案 - 將設置存儲在應用程序主機網站的列表中。
它由以下幾個功能組成。
CreateSettingsList:
Create創建一個包含Title和Value字段的普通列表,我用它來存儲設置名稱和與之關聯的值。 這在文檔就緒函數中被調用,以確保已經創建了一個列表,如果已經有一個列表,它會繼續並嘗試從中讀取。 如果之前不存在列表,我調用函數來初始化列表中的默認變量值。
//___________________________________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"
}
}
});
}
}
得到:
獲取讀取列表,找到您指定的設置,然后確定與該設置關聯的值“”或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"
}
}
});
});
}
這可以擴展為包括執行其他特殊任務的其他函數,例如,您可以創建一個“createSetting”函數,允許您動態添加新設置(我在初始問題中提到的要求之一)。 在我的例子中,初始化一組設置滿足了我的需求,但其他人可能想要一種方法來寫更多。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.