Update Custom Field Value using Java Script Object Model(JSOM)

In this post, I will explain you to Update Custom Field Value using Java Script Object Model(JSOM).Custom field can be update with different- different Model as i already discussed in my previous post.

Now here you will learn to Update Custom Field Value using Java Script Object Model(JSOM).

  1. Open Visual studio and create one SharePoint empty solution.
  2. Add Visual Web Part.
  3. Add some Jquery link on page as, I mentioned in image.

Update Custom Field Value using Java Script Object Model(JSOM)

 <script src="../_layouts/15/jquery-1.9.1.min.js"></script>
    <script type="text/javascript" src="../_layouts/15/sp.runtime.js"></script>    
    <script type="text/javascript" src="../_layouts/15/sp.js"></script>
    <script type="text/javascript" src="../_layouts/15/ps.js"></script>


<script type="text/javascript">
    var projectContext;
    var custom_Fields;
    var Allprojects;
    var customField_Data = [];

    $(document).ready(function(){        
        SP.SOD.executeOrDelayUntilScriptLoaded(ReadCustomFields, "PS.js");       
    });
    function ReadCustomFields() {
       
        projectContext = PS.ProjectContext.get_current();
        Allprojects = projectContext.get_projects();
        
        custom_Fields = projectContext.get_customFields();
        projectContext.load(custom_Fields);

        // Run the request on the server.
        projectContext.executeQueryAsync(getCF_Complete);
    }

    function getCF_Complete(response) {
        var cfEnumerator = custom_Fields.getEnumerator();


        // Save the details of each CF for later
        while (cfEnumerator.moveNext()) {
            var cf = cfEnumerator.get_current();


            customField_Data.push({
                Id: cf.get_id(),
                Name: cf.get_name(),
                InternalName: cf.get_internalName()
            });
        }
      
        SP.SOD.executeOrDelayUntilScriptLoaded( update_Project, "PS.js");
       
    }
    function update_Project() {
        var projectGUID = "9bf4a904-0df4-e611-80fe-00155d018621";
        var project = Allprojects.getById(projectGUID);
        var draft_Project = project.checkOut();
        var CustomfieldName = "JSOM Test Custom Field";

       
      
        var cfData = $.grep(customField_Data, function (val) {
            return val.Name === CustomfieldName;
        });

        if (cfData.length > 0) {
            draft_Project.setCustomFieldValue(cfData[0].InternalName, "Value Update By ProjectServerCode.com using JSOM");
        }       
        var publish_Job = draft_Project.publish(true);
        projectContext.waitForQueueAsync(publish_Job, 30, function (response) {
            if (response !== 4) {
                
            }
        });
    };
</script>

 


Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

error: Content is protected !!