سؤال

أحاول استخدام CSOM لاسترداد جميع الحقول المعروضة في طريقة عرض معينة.

أحتاج إلى معرفة نوع الحقل واسم داخلي الحقل.

لا أستطيع القيام به: giveacodicetagpre.

بسبب. gett_viewfields () فقط الحصول على الاسم الداخلي وليس نوع الحقل .. لذلك ما قمت به هو أولا سأحصل على جميع الحقول في قائمة، وكذلك الحقول في عرض وتكرر من خلال الحقولالقائمة وتحقق مما إذا كان ذلك أيضا في طريقة العرض، إذا كان الأمر كذلك، فأضفه إلى صفيفتي.

هذا هو ما لدي حتى الآن: giveacodicetagpre.

مشكلتي هنا هو هذا الخط: giveacodicetagpre.

عندما أفحص الأطباق المتغيرة، أجد القيم في الواقع في شيء مثل FielseInview. $ 2_0. $ 1_0 [0] ....

يمكنني إنشاء مجموعة أخرى واستخدم كل من مجالات الأمر CommandInview.getenumerator () وإضافة القيم إلى الصفيف الجديد، ثم التعامل معها .. ولكن هذا يبدو وكأنه تعامل مزدوج.

هل هناك خيار أفضل؟

هل كانت مفيدة؟

المحلول

Two considerations here:

  1. The jQuery inArray function is implemented by calling the native Array indexOf function, if available, otherwise it uses a for loop to cycle through the array elements in search of a match.

  2. Your current inArray call doesn't work because, as you've already realized, fieldsInView is a SPFieldCollection, which is an object that internally is really just an array of fields, but the array isn't part of the public API (any variable whose name starts with the $ sign is not meant to be used directly by client code, although, being javascript, nothing can stops you from accessing them, but it's not guaranteed that a future upgrade won't break your code, if you choose to do that).

Performance-wise, I wouldn't really care of first looping through the fieldsInView collection with the enumerator that you get from the getEnumerator function, in order to build an array of pure strings (fields internal names). Then I would use this newly built array as the second argument to the inArray function call. I think that the performance hit would be unnoticeable (how many fields you have in your view? If it some tenths, looping through them would take a fraction of a second).

Otherwise, you can use the lodash library _some function, which enables you to enumerate an array and passing each item to a callback function (which is your search match condition), but returns as soon as it finds a passing value and does not iterate over the entire collection.

نصائح أخرى

$.inArray function expects Array type to be passed, in your case fieldsInView variable is of SP.ViewFieldCollection type. So, in order to use fieldsInView in $.inArray it should be transformed into an Array, for example:

//Convert collection into array 
function collectionToArray(collection)
{
    var items = [];
    var e = collection.getEnumerator();
    while (e.moveNext()) {
       var item = e.get_current();
       items.push(item);                
    }
    return items;
}   

Example:

var context = SP.ClientContext.get_current();
var list = context.get_web().get_lists().getByTitle(listTitle);
var view = list.get_views().getByTitle(viewTitle);
var fieldsInView = view.get_viewFields();
context.load(fieldsInView);
var fields = list.get_fields();
context.load(fields);
context.executeQueryAsync(
    function(){
      var fieldsNames = collectionToArray(fieldsInView); //get field names array 

      var fieldEnumerator = fields.getEnumerator();
      var fieldArray = new Array();
      while (fieldEnumerator.moveNext()) {
        var field = fieldEnumerator.get_current();
        var fieldName = field.get_internalName();
        var found = $.inArray(fieldName, fieldsNames) > -1;
        //The remaining code is ommited for a clarity

      } 


    },
    function(sender,args){
          console.log(args.get_message());
    });

If you're enumerating through all the fields anyways why not just try something like this:

var internalNames = [];
var types = [];
var viewFieldEnum = viewFields.getEnumerator();
while (viewFieldEnum.moveNext()) {
  var currentViewField = viewFieldEnum.get_current();
  var allFieldsEnum = fields.getEnumerator();
  while (allFieldsEnum.moveNext()) {
    var currentField = allFieldsEmum.get_current();
    if (currentViewField === currentField.get_internalName()) {
      internalNames.push(currentField.get_internalName());
      types.push(currentField.get_fieldTypeKind());
    }
  }
}

It may not be the prettiest thing ever written, but would this work for you? Should get you the field internal names and their types.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى sharepoint.stackexchange
scroll top