11 December 2011

Update SharePoint fields on Client-Side with spservices

This time I will present a client-side code (JavaScript) - which updates a list of fields without having to insert the data (edit) into the form.

All you need is another class from beloved and familiar - jquery that Called- spservicehttp://spservices.codeplex.com/
It works in 2007 and 2010 because the code in the form ajax turns  to the sharepoint web-service. (Although in 2010 we have the "Client Object Model").
The following code built two objects - an image and textbox

I make an update of the field after clicking on the image, and then I prevent users from updating again.
Please note, I use the method of spservice SPUpdateMultipleListItems.
The method get the following variables:
webURL, listName, CAMLQuery, batchCmd, valuepairs, debug

  <input type="text" id="txt<%=this.UniqueID%>" class="myClass1" value="<%=likeValue%>" />
  <img class="myClass2" id="btn<%=this.UniqueID%>" src="/_layouts/images/img.gif" alt="" />
<script type="text/javascript">
    $(document).ready(function () {
        // The id$= is doing a regex match for an id ENDING with btn.
        var btn = $("img[id$='btn<%=this.UniqueID%>']");
        btnLike.click(function () {
            var txt = $("input[id$='txt<%=this.UniqueID%>']");
            var myValue = parseInt(txt.val()) + 1;
            var strVal = myValue.toString();
                    webURL: "<%=webUrl%>",
                    listName: "<%=listName%>",
                    CAMLQuery: "<Query><Where><Eq><FieldRef Name='ID'/><Value Type='Counter'><%=itemId%></Value></Eq></Where></Query>",
                    batchCmd: "Update",
                    valuepairs: [["<%=field1%>""..."], ["<%=field2%>", strVal]],
                    debug: true

Now you can update an item on every page with the content editor web-part that add the JS code.
I do not think the syntax is complicated,
If you have any questions, I'm here

Roi Kolbinger


Anonymous said...

Hi, I am a novice in sharepoint and I have following issue. Main problem is i do not know where to add code, somebody told me that it can be done through powershell and even gave me some code but do not know where and how to add it to my list.

Here is my problem:
So far, I know how to set list-settings, add columns and even click on edit-page:-), perhaps i can create a web-part as well; and even created the form for input.

I actually have or need to have three lists as below.

List-A, List-B and List-C

all of them have some lookup columns already and I am able to use simple lookup columns (meaning they get populated).

List-A: ObjectName, "DEV Contacts" and "PM Contacts"

List-B: DependencyName, ObjectName (single item lookup from List-A ObjectName), DependentObjectNames (multiple selection lookup from List-A ObjectName), "Service Contacts"

where "Service Contacts" = "DEV Contacts" and "PM Contacts" (for ObjectName in column-2 in this list, which is originall from List-A)

List-C: RecordType, DependencyNames (multiple item selection lookup from List-B DependencyName), ObjectNames(multiple item read-only lookup from List-B ObjectName (2nd column) that should be selected/cross-marked the moment you select dependency-name: check-mark corresponding object in Column-2 ofList-B when dependencyName is check-marked by user during List-C entry), DependentObjectNames (multiple item read-only lookup from List-B DependentObjectNames, that should be selected/cross-marked the moment you select dependency-name: similar to ObjectNames column but may be a bit difficult one-keep check-marking all DependentObjectNames that are there in the dependencyName just check-marked in column-2 of this list), DependencyServiceContacts (most difficult one: multiple item lookup from List-B for DEPENDENCY-NAMEs in column-2: this also keep on building/concatenating for each dependency that is check marked, so keep adding corressponding service contacts)

Processing for each lookup column in List-C is almost same, so if I know that this type of processing can be done (should be possible) and where to do this processing (web-part, or some script etc that needs to be deployed) then I can create logic for all of them.

So basic question is, if this type of control/calculations are feasible, and if yes what is the best possible mechanism.

I am a good enough C#/C++ programmer, so no problem in writing code if deployment etc is not going to be very troublesome.

I understand that I have given a complex/confusing scenario, so please let me know if something is not at all making sense:-)

my email-id is: firefightdd@yahoo.com

Roi Kolbinger said...

I think it's really possible with powershell-Create Assignment

$ MyAssignment = Start-SPAssignment Open the site requested by URL

$ MyWeb = Get-SPWeb http://MySite.com-AssignmentCollection $ MyAssignment stTemplates ["Custom List"])

Open the list

$ List1 = $ MyWeb.Lists ["Your List 1"]
$ List2 = $ MyWeb.Lists ["Your List 2"]

Turned to the field and tied it

$ Filed1 = $ List1.Fields ["YourField"]
$ List2.Fields.Add ($ Filed1)

Finally we close the site and the Assignment.

$ MyWeb.Dispose ()
Stop-SPAssignment $ MyAssignment

Also in C # console app it is possible and perhaps even easier key - similar code

Post a Comment