Skip to content

Commit 6712ae0

Browse files
waldekmastykarzVesaJuvonen
authored andcommitted
Added guidance on integrating web part properties with SharePoint (SharePoint#794)
1 parent ab8a730 commit 6712ae0

File tree

2 files changed

+69
-0
lines changed

2 files changed

+69
-0
lines changed
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
# Integrate web part properties with SharePoint
2+
3+
When building classic web parts, web part properties were isolated from SharePoint and their values were managed by end-users. SharePoint Frameworks offers you a new set of capabilities that simplify managing web part properties' values and integrate them with SharePoint Search. This article explains how you can use these capabilities when building SharePoint Framework client-side web parts.
4+
5+
> **Important:** The following guide applies only to SharePoint Framework client-side web parts placed on modern SharePoint pages. Capabilities described in this article don't apply to classic web parts or SharePoint Framework client-side web parts placed on classic pages.
6+
7+
## Client-side web part properties
8+
9+
When building SharePoint Framework client-side web parts, you can define properties that can be configured by users. By using properties instead of fixed values, web parts are more flexible and suitable for many different scenarios.
10+
11+
Comparing to classic web parts, there are some differences in how the SharePoint Framework handles web parts properties. The following schema illustrates how web part properties values flow through the different layers of SharePoint.
12+
13+
![Schema illustrating how the SharePoint Framework handles web part properties](../../../../images/integrate-webpart-properties-schema.png)
14+
15+
Before accepting values for web part properties from the end-users you should always [validate them](./validate-web-part-property-values). This will not only allow you to ensure that your web parts are user-friendly, but will also help you prevent storing invalid data in the web part's configuration. Additionally, you should consider that the SharePoint Framework doesn't support personalization and all users see the same configuration of the particular web part.
16+
17+
## Specify web part property value type
18+
19+
In classic SharePoint web parts, web part property values were isolated from SharePoint. If you had a web part property with a URL of a file stored in SharePoint, you had to manually ensure that this URL was valid and pointing to a correct document in case it was moved or renamed. Also, if you allowed users to enter some text to be displayed in the web part, that text wouldn't be indexed by SharePoint Search.
20+
21+
When building web parts, SharePoint Framework allows you to specify what kind of value the particular web part property holds. This configuration determines how SharePoint handles the value. Depending on the specified configuration, SharePoint can include the value of the particular property in the Search index, remove unsafe HTML and even keep links to documents stored in SharePoint up to date in case a file gets moved or renamed.
22+
23+
To specify the configuration for your web part properties, in the web part class, override the **propertiesMetadata** getter:
24+
25+
```ts
26+
import {
27+
BaseClientSideWebPart,
28+
IPropertyPaneConfiguration,
29+
PropertyPaneTextField,
30+
IWebPartPropertiesMetadata
31+
} from '@microsoft/sp-webpart-base';
32+
33+
// ...
34+
35+
export default class ArticleLinkWebPart extends BaseClientSideWebPart<IArticleLinkWebPartProps> {
36+
// ...
37+
protected get propertiesMetadata(): IWebPartPropertiesMetadata {
38+
return {
39+
'title': { isSearchablePlainText: true },
40+
'intro': { isHtmlString: true },
41+
'image': { isImageSource: true },
42+
'url': { isLink: true }
43+
};
44+
}
45+
// ...
46+
}
47+
```
48+
49+
The **propertiesMetadata** method returns an object, where the property is a string and specifies the name of the web part property and the value is an object specifying how SharePoint should handle that particular property. When overriding the **propertiesMetadata** method, you don't have to list all web part properties. By default, web part properties' values are not processed by SharePoint and you should include only those properties that you want to be processed.
50+
51+
Following is the list of possible values that can be set in the properties metadata and their impact on how the value of the web part property is processed by SharePoint.
52+
53+
Metadata value|Searchable|Link fixup|Remove unsafe HTML
54+
--------------|:--------:|:--------:|:----------------:
55+
none (default)|no|no|no
56+
`isSearchablePlainText`|yes|no|no
57+
`isHtmlString`|yes|yes|yes
58+
`isImageSource`|yes|yes|no
59+
`isLink`|yes|yes|no
60+
61+
> **Important:** when defining the configuration for your web part properties, you should use only one of the properties mentioned above for each web part property. Setting multiple properties will most likely lead to undesirable results and you should avoid it.
62+
63+
By default the value of a web part property is not indexed by SharePoint Search and it's not processed by SharePoint in any way. It's passed to the web part exactly how it's been entered by the user configuring the web part.
64+
65+
If you specify the web part property as `isSearchablePlainText`, it will be included in the full-text Search index. Whenever users search for any keywords included in the value of that property, SharePoint Search will return the page with the web part in the search results. If the value contains a link to a document stored in SharePoint, that link won't be updated if the referenced document is moved or renamed. Also, any HTML entered by users, will be kept intact. When working with the value of such property, you should treat it as plain-text and escape HTML that might be entered by users before rendering it on the page to avoid script injection.
66+
67+
When a web part property is defined as `isHtmlString`, SharePoint will first of all remove any unsafe HTML, such as `script` tags, from the property value. The HTML that remains can be considered safe to render on a page. If the value contains any URLs pointing to files stored in SharePoint, as soon as one of these files is renamed or moved, SharePoint will automatically update the URL stored in the web part property. This significantly simplifies managing URLs across all web parts and pages in your tenant. HTML web part properties are also searchable, so users can look for any keywords included in the property value.
68+
69+
Property value types `isImageSource` and `isLink` are meant to be used for web part properties than include nothing else but a link to an image or a file stored in SharePoint. In both cases, SharePoint Search includes the content in the full-text index and keeps the specified URL up-to-date in case the referenced file is renamed or moved. Additionally, image sources may get additional processing to help images download faster.
137 KB
Loading

0 commit comments

Comments
 (0)