【问题标题】:Store Properties for a SharePoint-hosted appSharePoint 托管应用程序的存储属性
【发布时间】:2014-07-22 00:38:54
【问题描述】:

我正在尝试找出在 SharePoint 托管应用程序中存储键/值对的最佳方式(或者实际上是任何可行的方式)。对需要:

  • 如果设置存在,则在启动时加载,否则使用默认值。
  • 按需创建——即用户可以在 UI 中添加新设置,然后我在代码中的其他位置使用该设置进行更改。例如,使用自定义文本字符串作为列表名称,而不是应用的默认设置。

我尝试使用 PropertyBag,但尝试写入时收到拒绝访问错误。

我也尝试使用列表,但无法让该技术正常工作。

有没有人建议一个好的方法以及如何完成。如果这些是最好的方法,我很乐意重新审视我已经尝试过的技术。

请记住,此问题应仅限于与 SharePoint 托管应用程序一起使用的问题。这意味着 C# 和服务器端代码都已出局。

【问题讨论】:

    标签: javascript csom sharepoint-online


    【解决方案1】:

    这是我最终使用的解决方案——将设置存储在应用程序主机网络的列表中。

    它由以下几个函数组成。

    创建设置列表:

    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"
                    }
                }       
            });
        }
    }
    

    获取:

    Get 读取列表,找到您指定的设置,然后确定与该设置关联的值“”或 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”功能,允许您即时添加新设置(我在最初的问题中提到的要求之一) .就我而言,初始化一组设置满足了我的需求,但其他人可能想要写更多的方法。

    【讨论】:

    • 只是想说声谢谢,我一直在为我的 SP 托管应用寻找解决方案,但遇到了完全相同的问题,我发现的只是写入 Web 配置文件和代码隐藏解决方案,但是如你所知;我们在 SP-Hosted 中得到的最接近的东西是 JS。所以谢谢,你的解决方案真的帮助了我:-)。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-04
    相关资源
    最近更新 更多