Create widgets in external assemblies
Sitefinity CMS enables you to create custom widgets outside of the SitefinityWebApp project and distribute them between projects as standalone .DLL files. Thus, the custom widget is contained in its own assembly, including its views. This brings convenience in terms of:
- Easier management of widgets and files when modifying them
- Versioning of widget assemblies
- Reuse widgets by distributing them as NuGet packages across multiple projects
- Optimization of folder structure
Create the project and the folder structure
When building an MVC widget in external assembly the first step is to add a class library to the Sitefinity solution in Visual Studio.
Next, build the folder structure of your project:
- Open your project in Visual Studio.
If needed, save the solution file.
- In the context menu of the solution, click Add » New Project…
- In the left pane, select Visual C# » Windows Desktop.
- Select Class Library and name it MessageWidget.
NOTE: You can also create the Class Library in the SitefinityWebApp project and not necessarily in an external project.
Add the core Project Feather and Sitefinity API to the class library. To do this, use the NuGet Package Manager and execute the following commands:
- Under MessageWidget class library, expand the Properties node and open the Assembly.cs file. Add the following:
- Inside the created class library, delete the class that is automatically created by Visual Studio.
- Reference your external project from the SitefinityWebApp project.
- Under the new class library, create a new folder and name it Mvc.
- Inside folder
Mvc, create three new folders - Controllers, Models, and Views.
- Under folder
Views, create a new folder and name it MessageWidget.
- Create the custom model, view, and controller. For detailed steps, see Create widgets.
Enable Visual Studio Code IntelliSense for MVC views in external assemblies
When creating the MVC views and .CSHTML files in an external assembly , the Code IntelliSense feature of Visual Studio is not enabled and you do not get the suggestions and autocomplete for your code.
NOTE: You need to make sure Telerik.Sitefinity.Feather.Core NuGet package in the class library project is the corresponding version as the one in the SitefinityWebApp project.
To enable Code IntelliSense in Visual Studio:
- Close all currently open .CSHTML files.
- In the context menu of the Class Library node, click Properties.
- Open the Build tab page and change the class library Output path to bin\.
NOTE: In case you complete this step for all build modes (Debug and Release, by default), the compiler will always output the .DLL files in ~/bin folder, instead of the default bin/Debug and bin/Release folders. As a result, all build configuration modes will output to the one and the same path. If, for the Release build, you do not modify the output path from bin/Release/ to bin/, Code IntelliSense is not enabled for the Release build.
- In the root of the project, create a web.config file.
NOTE: This web.config file is only used to enable the Code IntelliSense feature. All other configurations must be made in the web.config file of the SitefinityWebApp project.
- Paste the following code snippet:
- Clean and build the solution.
You can now create the custom model, controller, and view in the same way as you do with custom widgets you implement in the SitefinityWebApp project. For more information, see Create widgets.
For a full sample demonstrating how to create a widget in an external assembly, check out the KendoBooksWidget sample in the GitHub repository.