You can use the SharePoint client object model to retrieve, update and manage data in SharePoint 2013. SharePoint makes the object model available in several forms.
- .NET Framework redistributable assemblies
- JavaScript library
- REST/OData endpoints
- Windows Phone assemblies
- Silverlight redistributable assemblies
This article shows how to do basic operations using the JavaScript object model. You can add a reference to the object model using HTML <script> tags.
The following sections describe tasks that you can complete programmatically and they include JavaScript code examples that show the operations.
To create files, you use a FileCreationInformation object, set the URL attribute and append content as a base64 encoded array of bytes, as shown in this example.
Procedure
Open your SP Site in SharePoint 2013 Designer, then select Site Assets icon in the designer ribbon and add a JavaScript file.
![]()
Figure 1: Add a JavaScript File
After adding a JavaScript file, this file will be available in the SharePoint Site Assets Folder.
![]()
Figure 2: Site Assets Folder
Go to the SP site Page and add a new page to the SharePoint site.
![]()
Figure 3: SP Site
![]()
Figure 4: Add a New Page
After adding a page select an Insert button in the Ribbon Menu.
![]()
Figure 5: Ribbon Menu
Then add a Content Editor Web part into the page.
![]()
Figure 6: Content Editor
Edit the WebPart and add a JavaScript file link to the content link .
![]()
Figure 7: Web Part
Save the web part and save the page in the site. Click the button!
![]()
Figure 8: Save the web Part
![]()
Figure 9: list updated succesfully
List updated successfully.
Source Code
- < div > < button onclick = " createfile ()" > Click here to createfile < /button></div >
- < div id = " resultpanel " > < /div>
- <script type="text/javascript
- ">
- function createfile (resultpanel)
- {
- var clientContext;
- var oWebsite;
- var oList;
- var fileCreateInfo;
- var fileContent;
- clientContext = new SP.ClientContext.get_current();
- oWebsite = clientContext.get_web();
- oList = oWebsite.get_lists().getByTitle("
- Shared Documents ");
- fileCreateInfo = new SP.FileCreationInformation();
- fileCreateInfo.set_url("
- my new file.txt ");
- fileCreateInfo.set_content(new SP.Base64EncodedByteArray());
- fileContent = "
- The content of my new file ";
- for (var i = 0; i < fileContent.length; i++)
- {
- fileCreateInfo.get_content().append(fileContent.charCodeAt(i));
- }
- this.newFile = oList.get_rootFolder().get_files().add(fileCreateInfo);
- clientContext.load(this.newFile);
- clientContext.executeQueryAsync(
- Function.createDelegate(this, successHandler),
- Function.createDelegate(this, errorHandler)
- );
- function successHandler()
- {
- resultpanel.innerHTML =
- "
- Go to the " +
- " < a href = '../Lists/Shared Documents' > document library < /a> " +
- "to see your new file.";
- }
-
- function errorHandler()
- {
- resultpanel.innerHTML = "Request failed: " + arguments[1].get_message();
- }
- }
- </script >
Thanks for reading my article.