Friday, 11 May 2012

The Whole Thing

Replace "<<" with "<" and ">>" with ">"
then save as a HTML file and load into a sharepoint web part

Change the variales at the topto point to your own sharepoint list

 <<style type="text/css">>
 Button {

  background:#FFFFFF;
  height:50px;
  width:100px;
  background.colour:#AAAAAA;
  margin:0;
  padding:0;
  cursor:pointer;
 
  border-style:hidden;
  border-spacing:0px;
  border-width:0px;
 
  font-size:9px;
  font-family:Verdana;
  font-weight:bold;
  }
  .TxtBlk{
   color:#330000;
   }
  .TxtWht{
   color:#FFFFFF;
   }
  .BtnR{
   color:#FFFFFF;
   }
 <</style>>

 <<script type="text/javascript">>
  // REMEMBER THIS ONLY WORKS WHEN ITS IN A WEB PART PAGE IN A CONTENT EDITOR WEB PART
  var strReplace = null;
   var siteUrl = '/sites/10066';  //Enter your site details here. remember its the site we want, not the "list" folder in the site we want
           // You basically remove the first bit "https://a.b.c
   var strList = 'TestList';  //Enter the name of the list
   var strField = 'Title';   //Enter the name of the field you want to change
   var strTextSet = 'Set';  //Enter the test you want to toggle
   var strTextReset = 'Reset';
  
   function UpdateAllThat(strThisThat)
    {
    strReplace = strThisThat;
    //alert ("Update All " + strReplace + " , " + strList + " , " + siteUrl);
    var context = new SP.ClientContext(siteUrl);
    //var context = new SP.ClientContext.get_current();
    var web = context.get_web();
    var list = web.get_lists().getByTitle(strList);

    var query = SP.CamlQuery.createAllItemsQuery();
    allItems = list.getItems(query);
     var strTemp = "'Include(ID,Title,Department)')";
    context.load(allItems, 'Include(ID,Title,Department)');
    context.executeQueryAsync(Function.createDelegate(this, this.UpdateAllThatSuccess), Function.createDelegate(this, this.failed));

    }
   function UpdateAllThatSuccess()
   {
    //alert ("UpdateAllThatSuccess " + strReplace);
    var ListEnumerator = this.allItems.getEnumerator();
 
    while(ListEnumerator.moveNext())
    {
     var thiscurrentItem = ListEnumerator.get_current();
     var strthiscurrentItemID  = thiscurrentItem.get_item('ID');
     var strthiscurrentItem  = thiscurrentItem.get_item(strField);
     //alert ("strthiscurrentItem = " +  strthiscurrentItem + "," + strthiscurrentItemID  );
     Update(strthiscurrentItemID,strReplace);
    }
    //alert ("UpdateAllThatSuccess");
   }
   function Update(intItemNumber,strReplace)
   {
    //alert ("#1 Update " + strReplace + intItemNumber);
    var Updatecontext = new SP.ClientContext(siteUrl);
    //var Updatecontext = new SP.ClientContext.get_current();
    var Updateweb = Updatecontext.get_web();
    var Updatelist = Updateweb.get_lists().getByTitle(strList);
    var UpdatecurrentItem = Updatelist.getItemById(intItemNumber);
   
    UpdatecurrentItem.set_item(strField,strReplace + intItemNumber);
    UpdatecurrentItem.update();
    Updatecontext.executeQueryAsync(Function.createDelegate(this, this.UpdateSuccess(strReplace + intItemNumber)), Function.createDelegate(this, this.failed));
    //alert ("#2 Update " + strReplace + intItemNumber);
  
   }
   function UpdateSuccess(strString)
   {
    document.getElementById("aQ3").innerHTML = "Update Success - " + strString;
   }  
   function failed(sender, args)
   {
    alert("failed. Message:" + args.get_message());
   }  
   function DisplayButton()
   {
  
    var btnStart = "<<button type='button' ";
    var btnEnd =  "<</button>>";
    var ButtonStyleRed="class=' BtnR'";
   
    var QL1onClick =" onClick=UpdateAllThat(strTextSet)";
    var QL1ToolTip = " title='Set All Items'";
    var QL1btnEnd = ">>Set All<<br>>That<</button>>";
    var QL2onClick =" onClick=UpdateAllThat(strTextReset)";
    var QL2ToolTip = " title='Reset All Items'";
    var QL2btnEnd = ">>Reset All<<br>>This<</button>>";
    document.getElementById("aQ1").innerHTML = btnStart + ButtonStyleRed + QL1ToolTip + QL1onClick + QL1btnEnd;
    document.getElementById("aQ2").innerHTML = btnStart + ButtonStyleRed + QL2ToolTip + QL2onClick + QL2btnEnd;
   
   }
  <</script>>
  <<table>>
   <<tr>>
    <<td id="aQ1">>Q1<</td>>
    <<td id="aQ2">>Q2<</td>>
    <<td id="aQ3">>Click on a button to Set / Reset <</td>>
   <</tr>>
  <</table>>
  <<script type="text/javascript">>
   DisplayButton();
  <</script>>

3 comments:

  1. I put it in a web part page in a content editor part and it doesn't work. Do you add something else? When I put the site, I put like "http://mysite", it's correct? Thanks

    ReplyDelete
  2. Hi Albert

    Er - the solurtion does work - I use it daily on a list of 1800 items

    my (new) web page has a downloadable file

    https://sites.google.com/site/thesecretbulmer/

    that has a downloadable copy - you need to reference the code as a file in the Web part properties

    ReplyDelete
  3. Hi Albert. you do need to change teh variables to point to your list, and you dont place it in a content editor, you create an html file, upload it and point to teh html from the content editor

    ReplyDelete