It is possible to provision forms programmatically using Plumsail.Forms.O365 NuGet package.
Important
For SharePoint 2019, please, use the Plumsail.Forms.SP2019 NuGet package instead.
In order to use the package, you need to have .NET Framework v.4.8 installed.
Find an example of how it can be used in our article - Provision Modern UI SharePoint Form.
Constructor |
Description/Examples |
---|---|
FormsManager(ClientContext ctx, Guid listId, string contentTypeId); |
Forms Manager allows you to manage modern SharePoint Forms for a specific Content Type in the specific List. It takes three arguments as parameters: Client Context for SharePoint Site with valid Site Owner credentials, the ID of the List, and the ID of the Content Type as a string. ctx - client context for the site that you try to access, with Full Control Credentials. listId - ID of the list that you try to access. contentTypeId - string with the Content Type Id. Example: public static void Main(string[] args) {
// Application (client) ID
const string azureAppId = "00000000-0000-0000-0000-000000000000";
// Directory (tenant) ID
const string tenantId = "00000000-0000-0000-0000-000000000000";
// Path to the app certificate
const string certificatePath = @"c:\provision\cert.pfx";
// Certificate password
const string certificatePassword = "password";
// Path to the exported form
const string layoutFilePath = @"c:\provision\Item (New).json";
// URL of the site
const string siteUrl = "https://your-domain.sharepoint.com/sites/your-site";
// List title
const string listTitle = "List";
// Content type name
const string contentTypeName = "Item";
var authManager = AuthenticationManager.CreateWithCertificate(azureAppId, certificatePath, certificatePassword, tenantId);
using
var ctx = authManager.GetContext(siteUrl);
// If you want to authenticate using login and password, use SharePointOnlineCredentials class instead:
//
// using var ctx = new ClientContext(siteUrl);
// ctx.Credentials = new SharePointOnlineCredentials("your-login@your-domain.onmicrosoft.com", GetSecureString("qwerty"));
ctx.Load(ctx.Web);
ctx.ExecuteQuery();
// Specify a list which form you want to replace
var list = ctx.Web.Lists.GetByTitle(listTitle);
var contentTypes = list.ContentTypes;
ctx.Load(list);
ctx.Load(contentTypes);
ctx.ExecuteQuery();
var contentType = contentTypes.FirstOrDefault(ct => ct.Name == contentTypeName);
var forms = new FormsManager(ctx, list.Id, contentType.Id.ToString());
//use FormsManager methods here...
}
private static SecureString GetSecureString(string s) {
var result = new SecureString();
foreach(var c in s.ToCharArray()) {
result.AppendChar(c);
}
return result;
}
|
Method |
Description/Examples |
---|---|
GenerateForms(Guid formSetId, FormTypes formTypes, layout) |
Generates specified (New, Edit or Display) form for the specific Form Set. Takes 3 arguments: formSetId - ID of the Form Set(empty Guid for Default). formTypes - Flags indicating one or more form types. layout - layout of the form. Example: const string layoutFilePath = @"c:\provision\Item (New).json";
var layout = System.IO.File.ReadAllText(layoutFilePath);
forms.GenerateForms(
Guid.Empty,
FormTypes.New | FormTypes.Edit | FormTypes.Display,
layout
);
|
GetFormSets() |
Allows to get Form sets for the List. Returns FormSetSettings. Important Only available after customization of Form sets, for example, adding new Form Set or customizing the Panel. Example: var settings = forms.GetFormSets();
|
GetLayout(Guid formSetId, FormTypes formType, LayoutType layoutType) |
Allows to get specified form’s layout from the List for the form set. The layout can be used with GenerateForms() method, instead of getting layout from exported file. Takes 3 arguments: formSetId - ID of the Form Set (empty Guid for Default). formType - a Flag indicating one form type. Either FormTypes.New, FormTypes.Edit, or FormTypes.Display. layoutType - the format of the layout string returned by the method. Either LayoutType.Xml or LayoutType.Json. Example: var layoutNew = forms.GetLayout(Guid.Empty, FormTypes.New);
var layoutEdit = forms.GetLayout(Guid.Empty, FormTypes.Edit);
var layoutDisplay = forms.GetLayout(Guid.Empty, FormTypes.Display);
var layoutNewJson = forms.GetLayout(Guid.Empty, FormTypes.New, LayoutType.Json);
|
ResetForms(Guid formSetId, FormTypes formType) |
Allows to reset the specified form for the specific form set in the List to the default. Takes 2 arguments: formSetId - ID of the Form Set(empty Guid for Default). formTypes - Flags indicating one or more form types. Example: // reset the default New Form:
forms.ResetForms(Guid.Empty, FormTypes.New);
|
SetFormSets(FormSetSettings settings) |
Allows to use FormSetSettings to create a structure for Form sets in the List. Still need to generate forms after. Takes 1 arguments: settings - settings for routing, including rules and logic. Example: var settings = formsOldSite.GetFormSets();
formsNewSite.SetFormSets(settings);
//alternatively create new Form Set settings
formsNewSite.SetFormSets(new FormsDesigner.Data.SharePoint.FormSetSettings() {
//use Constructor to set properties
})
|
FormSetSettings can be retrieved with GetFormSets() and set with SetFormSets(FormSetSettings).
These settings contain code for Custom routing, as well as information about Form sets, including groups used for redirection.
For examples of working with Form sets, please, check out Working with Form sets when Provisioning.
Properties |
Description/Examples |
---|---|
CustomRouting |
Contains string with logic for custom routing. Can be used to get and set. Example: var fss = forms.GetFormSets();
var routing = fss.CustomRouting;
|
FormSets |
Contains IEnumerable of Form sets. Can be used to get and set. Returned Form Set class contains: ExcludedGroupIds - IEnumerable of excl. group IDs (ints). IncludedGroupIds - IEnumerable of incl. group IDs (ints). Order - int order of the form set. Title - string title of the form set. Id - guid formSetId, can be used with GenerateForms(), GetLayout(), etc. Example: var fss = forms.GetFormSets();
var sets = fss.FormSets;
foreach (var Set in sets)
{
var exclude = Set.ExcludedGroupIds;
var include = Set.IncludedGroupIds;
var order = Set.Order;
var title = Set.Title;
var guid = Set.Id;
}
|
Panel |
Contains object that determines which forms will open in a panel and at what size. Can be used to get and set. New, Edit and Display are all properties that specify each form’s settings. If not specified - specific form is automatically set to null, and not shown in a panel. Example: var fss = forms.GetFormSets();
fss.Panel = new FormTypePanelSettings()
{
Display = new FormPanelSettings()
{
Size = FormPanelSize.Medium
},
Edit = new FormPanelSettings()
{
Size = FormPanelSize.Large
},
New = null
};
|
CustomListViewStyle |
Contains string with custom CSS code for the List View of the selected list. Can be used to get and set. Example: var fss = forms.GetFormSets();
ffs.CustomListViewStyle = ".ms-CommandBarItem-link[data-automationid='detailsPane'] { display: none; }";
|