I have a document library where I added a text column Read
. I'm using a script editor web part to insert some JavaScript. I need to get the value of this column Read
for a specific item (I know the item Id).
I would appreciate any help. Thank you!
I have a document library where I added a text column Read
. I'm using a script editor web part to insert some JavaScript. I need to get the value of this column Read
for a specific item (I know the item Id).
I would appreciate any help. Thank you!
Share Improve this question edited Jul 8, 2014 at 0:39 JasonMArcher 15k22 gold badges59 silver badges53 bronze badges asked Jan 13, 2014 at 19:43 KateKate 1,4954 gold badges21 silver badges36 bronze badges4 Answers
Reset to default 2This code worked:
this.oListItem = oList.getItemById(itemid);
ctx.load(this.oListItem);
ctx.executeQueryAsync(Function.createDelegate(this,
function () {prevText = this.oListItem.get_item('Read'); }),
function (sender, args) { alert('Error occured' + args.get_message());
});
You should use Sharepoint client object model to get the document library where you added the text column. After that you can get the list item by building a caml query with the item id and you can get the value of your column.
You may want to use SPServices or SharepointPlus to help you. With SharepointPlus the code would be:
$SP().list("Name of your list").get({fields:"Read",where:"ID = 123"}, function(data) {
if (data.length===1) alert("Read = "+data[0].getAttribute("Read"))
else alert("Unable to find ID 123")
})
Using SPServices and jQuery, you can reference those in your code:
<script type="text/javascript" src="jquery-1.12.4.min.js"></script>
<script type="text/javascript" src="jquery.SPServices.min.js"></script>
Then get the fields of the current item using this code:
<script type="text/javascript">
$(document).ready(function() {
// Don't forget to replace the document library's name
// if it's not "Documents"...
getListItems('Documents',
function(items){
var e = items.getEnumerator();
while (e.moveNext()) {
var item = e.get_current();
//console.log(item.get_item('FileLeafRef')); //print File or Folder Name
//console.log(item.get_item('FileRef')); //print File or Folder Url
var read = item.get_item('Read');
alert('Read = ' + read);
}
},
function(sender,args){
console.log(args.get_message());
}
);
});
function getListItems(listTitle,success,error)
{
//alert('getting items');
var context = SP.ClientContext.get_current();
var list = context.get_web().get_lists().getByTitle(listTitle);
var items = list.getItems(SP.CamlQuery.createAllItemsQuery());
context.load(items);
context.executeQueryAsync(
function() {
success(items);
},
error
);
}
</script>