The example in this topic show how to use JSOM to Disable Attachement in SharePoint
- You can use Content Editor or Script Editor web part
on your site, put Html and
JSOM (Java Script Object Model) code and that's it. You can perform any action supported by JSOM APIs
How to execute JSOM code in SharePoint?
- Note: This is just a sample, you can go ahead with your
implementation.
- Open your Site. Create a webpart page.
- Put a Content Editor Web part and a Script Editor Web part
on it.
- I have added below Html in Content Editor webpart.
You can replace MainFunction with your actual method name.
<button onclick="MainFunction();"
type="button">Click
me</button>
- And below references in Script Editor Webpart.
You can keep jQuery reference as per your need. ScriptFile.js is a JavaScript
file where you can keep below function i.e. your actual logic.
<script src="https://code.jquery.com/jquery-2.2.4.js"
type="text/javascript"></script>
<script type="text/javascript"
src="/_layouts/15/sp.runtime.js"></script>
<script type="text/javascript"
src="/_layouts/15/sp.js"></script>
<script src="https://MyServer/sites/SiteCollection/style
library/js/ScriptFile.js"></script>
- Once you are done with setting your page, click on the button and your method should
get executed.
- function DisableAttachement() {
- // You can optionally specify the Site URL here to get
the context
- // If you don't specify the URL, the method will get
the context of the current site
- // var clientContext = new
SP.ClientContext("http://MyServer/sites/SiteCollection");
- var clientContext = new
SP.ClientContext();
-
- var listCollection =
clientContext.get_web().get_lists();
- var list = listCollection.getByTitle("List Name");
-
- // Once you "Disable" the attachement in
Sharepoint list, all the existing attachments on list items will be removed.
- // If you wannt
to "Disable" the attachment, pass "false" in below line
-
list.set_enableAttachments(false);
-
-
list.update();
-
- // Execute the query to the server.
-
clientContext.executeQueryAsync(Function.createDelegate(this, this.onsuccess),
Function.createDelegate(this, this.onfailed));
- }
-
- function onsuccess() {
-
console.log("success");
- }
-
- function onfailed(sender, args) {
-
console.log('Creation Failed' +
args.get_message() + '\n' +
args.get_stackTrace());
- }
Thank you for reading this article. This code was tested in SharePoint 2013