1

Question regarding the client object model. I wrote some client script for a procedure, but i don't know, how can I determine if the list or item has the field available or not? For example:

this.listItem.get_item("_dlc_DocId");

This code will execute very wel if the field "_dlc_DocID" is available in the list, but it is also possible that this field will not be available because of the feature that is deactivated, then it will throw an error. And nothing will execute further, so i need some kind of a check to see if this field is available or not. Anyone has any idea? I tried:

list.get_fields().GetByInternalNameOrTitle("_cld_DocID")

But no success ... do i miss something?

Here is the whole code:

function CreateLink() {
    var ctx = SP.ClientContext.get_current();
    this.web = ctx.get_web(); 
    this.site = ctx.get_site();
    var listId = SP.ListOperation.Selection.getSelectedList();
    this.list = this.web.get_lists().getById(listId);

    var items = SP.ListOperation.Selection.getSelectedItems(ctx);
    var mijnid = items[0];
    this.listItem = list.getItemById(mijnid.id);

    ctx.load(this.list);
    ctx.load(this.listItem);
    ctx.load(this.web, 'ServerRelativeUrl');
    ctx.load(this.site, 'ServerRelativeUrl', 'Url');
    ctx.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));
}

function onQuerySucceeded(sender, args) {

    var title = this.listItem.get_item('FileLeafRef');
    var webRelativeUrl = this.web.get_serverRelativeUrl();
    var siteUrl = this.site.get_url();
    var webAppUrl = siteUrl.replace(webRelativeUrl, "");
    var url;    
    var dlcdocid=this.list.get_fields().getByInternalNameOrTitle("_dlc_DocIdSome");

   .... todo
 }

2 Answers 2

3

Found a proper javascript client object model solution for my problem: Here is the solution:

this.listFields = list.get_fields();
ctx.load(this.listFields);

And then in the function: onQuerySucceeded

var fieldEnumerator = this.listFields.getEnumerator();
    while (fieldEnumerator.moveNext()) {
        var field = fieldEnumerator.get_current();
        var name = field.get_staticName();
        if (name == "_dlc_DocId") {
            docFeatureEnabled = true;
            break;
        }
    }

I hope this helps and can spare some time for people.

0
string siteUrl = "http://MyServer/sites/MySiteCollection"; 
        ClientContext clientContext = new ClientContext(siteUrl); 
        SP.List List = clientContext.Web.Lists.GetByTitle("my forum"); 
        for (int i = 0; i < list.Fields.Count; i++) 
         { 
           if (list.Fields[i].Title == "xyz") 
           { 
              - 
              - 
            } 
          } 

I have stolen this from https://stackoverflow.com/questions/4401253/check-if-a-list-column-exists-using-sharepoint-client-object-model

6
  • i know i tried this, but it doesn't work :(
    – Shkipper
    Sep 10, 2012 at 9:47
  • on this line: for (int i = 0; i < list.Fields.Count; i++) i get error missing ";" :(
    – Shkipper
    Sep 10, 2012 at 9:47
  • and even if im in debug mode and i try to add list.Fields it says no such property is defined :(
    – Shkipper
    Sep 10, 2012 at 9:49
  • does list.get_fields() get you anything? you mentioned in your post you had tried this? If it does then you can count the fields that this returns and replace the list.Fields.Count with an interger variable.
    – Truezplaya
    Sep 10, 2012 at 10:11
  • yeah it returns an object, but i dont know ... if i enumarte with it like for(var f in list.get_fiels()) then it returns like all methods and properties etc :/
    – Shkipper
    Sep 10, 2012 at 10:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.