Site selector
The following tutorial demonstrates how to add a Site selector in your widget's designer. You use the Siteselector to select different sites defined in your Sitefinity CMS application.
Add Site selectors
-
Sitefinity CMS automatically registers the scripts you need and, if no other designer view with explicitly set priority exists, Sitefinity CMS sets your designer view priority 1. In case you need to have full control over the scripts that are loaded or you want to set custom priority, you can alternatively create your own
DesignerView.YourView.jsonfile. If you have aJSONfile that matches the convention (even if empty), this automatic scripts registration will not occur. In yourDesignerView.<YourView>.jsonfile, add ascriptsarray. The content of the file should be similar to the following:JSON{ "priority": 1, "components" : ["sf-site-selector"] }For more information on the scripts that you must load, see List of selectors scripts reference.
-
Sitefinity CMS automatically finds all AngularJS modules you rely on and references the widget designer to them. In case you rely on custom AngularJS modules or have logic that needs an AngularJS controller, you can create your own
designerview-<yourview>.jsfile. If you have a.jsfile that matches the convention (even if empty), this automatic modules referencing will not occur. In yourdesignerview-<yourview>.jsfile, place the following code in your designer's controller:JavaScript(function () { //add the following snippet before the definition of the custom controller: var designerModule = angular.module('designer'); angular.module('designer').requires.push('sfSelectors'); //definition of the custom controller designerModule.controller('SettingsCtrl', ['$scope', 'propertyService', function ($scope, propertyService) { $scope.feedback.showLoadingIndicator = true; propertyService.get() .then(function (data) { if (data) { $scope.properties = propertyService.toAssociativeArray(data.Items); $scope.sfSite = $.parseJSON($scope.properties.Site.PropertyValue); } }, function (data) { $scope.feedback.showError = true; if (data) $scope.feedback.errorMessage = data.Detail; }) .finally(function () { $scope.feedback.showLoadingIndicator = false; }); $scope.$watch('sfSite', function (newValue, oldValue) { if (newValue) { $scope.properties.Site.PropertyValue = JSON.stringify(newValue); } }); }]); })();In the code above, you use the
propertyServiceto load the properties of your widget. Next, you create a scope property to hold theSitevalue. You can watch for any changes in the scope to update the widget properties. For more information about getting and setting the selected item, see Use content items selectors. -
In your
DesignerView.<YourView>.cshtmlfile, place the following tag where you want to render the Siteselector: ```HTML+Razor> info **NOTE**: If you do not supply a value for the `sfSite` attribute, the *Site* selector selects the default Sitefinity CMS backend site. In addition, if Sitefinity is in *single site* mode the *Site* selector is not rendered.
Get or set the selected site
To access the selected site, you use the sf-site attribute. You must add the following property in your widget's controller:
using BooksWidget.Mvc.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Telerik.Sitefinity.Mvc;
namespace BooksWidget.Mvc.Controllers
{
[ControllerToolboxItem(Name = "Books", SectionName = "MVC samples", Title = "Books")]
public class BookController_DatePickerSiteSelector : Controller
{
//custom logic
public string Site
{
get;
set;
}
}
}
The value of the sf-site attribute is the sfSite scope property, which has the following JSON structure:
{
"CultureDisplayNames" : [values],
"Id" : [Guid],
"IsAllowedConfigureModules" : [false/true],
"IsAllowedCreateEdit" : [false/true],
"IsAllowedSetPermissions" : false,
"IsAllowedStartStop" : false,
"IsDefault" : false,
"IsDeleteable" : false,
"IsOffline" : false,
"Name" : "site2",
"SiteMapRootNodeId" : [Guid],
"SiteUrl" : [Url],
"UIStatus" : "[Online/Offline]"
}