Search This Blog

Saturday, July 29, 2017

To Get File And Folder From Document Library Using JSOM From To Way



1- Without Caml Querry
2- With Caml Query


var LibraryNameForUpdate='NGODocument';
1-  Without Caml Querry

function FileWithContentTypeNotUsedCAMLQuery(folderName, selectedLink)
{
var context = new SP.ClientContext.get_current();
var web = context.get_web();
var list = web.get_lists().getByTitle(LibraryNameForUpdate);
var query = SP.CamlQuery.createAllItemsQuery();
query.set_folderServerRelativeUrl(folderName);
allItems = list.getItems(query);
context.load(allItems, 'Include(Title,ContentType,File,Author,Editor,Modified)'); // If You want Specific column is Loaded Then write This
//context.load(allItems, 'Include(ID,Title,Description0,File,Author,Editor,Modified,_ModerationStatus,Created)');
//context.load(allItems);  If You want All  column is Loaded Then write This

context.executeQueryAsync(Function.createDelegate(this, this.successGetViewAllFileApprovedReject),   Function.createDelegate(this, this.failedsuccessGetViewAllFileApprovedReject));

}

function successGetViewAllFileApprovedReject()
{

  var fileUrls = "";
  var ListEnumerator = this.allItems.getEnumerator();
   var count = 0;
   while(ListEnumerator.moveNext())
   {
    var currentItem = ListEnumerator.get_current();
    var _contentType = currentItem.get_contentType();
   
     if(_contentType.get_name() != "Folder")
       {
         debugger;
      // count = count + 1;
        var File = currentItem.get_file();
        var fieldUserValueCreatedBy = currentItem.get_item("Author");
        var fieldUserValueModifiedBy = currentItem.get_item("Editor");
        var currentdate = new Date();
        var modifiednumberofDaysago = showDays(currentdate.toISOString(), currentItem.get_item('Modified'));
        if(File != null)
          {
         debugger;
          fileUrls += File.get_serverRelativeUrl() + '\n';
         
          }
       }
   }
//alert(fileUrls);
  //alert(count);
}
function failedsuccessGetViewAllFileApprovedReject(sender, args) {
  alert("failed. Message:" + args.get_message());
}



1-  With Caml Querry (It best Way To Use )


function FileWithCAMLQuery(folderName, selectedLink)
{
   debugger;
    var context = new SP.ClientContext.get_current();
                var web = context.get_web();
                var list = web.get_lists().getByTitle(LibraryNameForUpdate);
                var query = new SP.CamlQuery();
                query.set_folderServerRelativeUrl(folderName);
                var q = '<View Scope=\'Recursive\'><Where><Eq><FieldRef Name=\'FSObjType\'/><Value Type=\'Number\'>0</Value></Eq></Where></View>';
                query.set_viewXml(q);
                allItems = list.getItems(query);
                //this.listInfoArray=context.load(allItems);If You want All  column is Loaded Then write This
                context.load(allItems, 'Include(ID,Title,Description0,File,Author,Editor,Modified,_ModerationStatus,Created)'); // If You want Specific column is Loaded Then write This

                context.executeQueryAsync(Function.createDelegate(this, this.successFileWithCAMLQuery), Function.createDelegate(this, this.failedsuccessFileWithCAMLQuery));

}

function successFileWithCAMLQuery()
{
debugger;
  var fileUrls = "";
  var ListEnumerator = this.allItems.getEnumerator();
   var count = 0;
   while(ListEnumerator.moveNext())
   {
       var currentItem = ListEnumerator.get_current();
      //  debugger;
         count = count + 1;
         var File = currentItem.get_file();
         var fieldUserValueCreatedBy = currentItem.get_item("Author");
         var fieldUserValueModifiedBy = currentItem.get_item("Editor");
         var currentdate = new Date();
         var modifiednumberofDaysago = showDays(currentdate.toISOString(), currentItem.get_item('Modified'));
         var dateCreated = new Date(currentItem.get_item('Created'));
         console.log(dateCreated.getDate() + "-" + monthNames[dateCreated.getMonth()] + "-" + dateCreated.getFullYear());
         console.log(fieldUserValueCreatedBy.get_lookupValue());
         console.log(fieldUserValueModifiedBy .get_lookupValue());
         console.log(currentItem.get_item('Title'));
         console.log(currentItem.get_item('Description0'));
         console.log(currentItem.get_item('ID'));
         console.log(currentItem.get_item('_ModerationStatus'));
      
   }
    //alert(modifiednumberofDaysago);
     alert(count);
}
function failedsuccessFileWithCAMLQuery(sender, args) {
  alert("failed. Message:" + args.get_message());
}


****************************All Field Get By this Internal Name*********************************
LIST OF INTERNAL NAMES FOR SHAREPOINT FIELDS

In order to reference a column or field using the the SharePoint object model, you often need to know its internal name. For example, when creating a CAML query, you can specify the field on which to search by providing its internal name. As opposed to the display name, which can be changed in the UI, the internal name is immutable. While the default display name and the internal name are often identical or similar, they can also be very different. The difference isn’t always consistent. For example, spaces in the display name can be removed, such as IsCheckedoutToLocal, or replaced with the hexadecimal equivalent, such as HTML_x0020_File_x0020_Type. Furthermore, the display name can be the same for more than one fields, so the internal name is the only way to distinguish between them.
You can quickly determine the internal name of a field using the UI:
1.      Open the List Settings page
2.      Under the Columns section, select a column to view the Edit Column page
3.      The URL of this page includes the internal name in the query string. For example, the URL for the Created By field includes the following query string List=%7BF641CEF1%2DCDE2%2D49E1%2D9800%2D861A408EF890%7D&Field=Author. The value for the Field parameter, Author, is the internal name for Created By.
However, this approach isn’t always convenient. If the column you want doesn’t already belong to the list, you’d have to first add it.
For reference purpose, below is the list of the display name and corresponding internal name for the default fields of a Document Library.
TITLE
INTERNAL NAME
Approval Status
_ModerationStatus
Approver Comments
_ModerationComments
Check In Comment
_CheckinComment
Checked Out To
CheckoutUser
Checked Out To
CheckedOutTitle
Checked Out To
LinkCheckedOutTitle
Content Type
ContentType
Content Type ID
ContentTypeId
Copy Source
_CopySource
Created
Created
Created
Created_x0020_Date
Created By
Author
Document Created By
Created_x0020_By
Document Modified By
Modified_x0020_By
Edit
Edit
Edit Menu Table End
_EditMenuTableEnd
Edit Menu Table Start
_EditMenuTableStart
Effective Permissions Mask
PermMask
Encoded Absolute URL
EncodedAbsUrl
File Size
File_x0020_Size
File Size
FileSizeDisplay
File Type
File_x0020_Type
GUID
GUID
Has Copy Destinations
_HasCopyDestinations
Html File Link
xd_ProgID
HTML File Type
HTML_x0020_File_x0020_Type
ID
ID
ID of the User who has the item Checked Out
CheckedOutUserId
Instance ID
InstanceID
Is Checked out to local
IsCheckedoutToLocal
Is Current Version
_IsCurrentVersion
Is Signed
xd_Signature
Item Type
FSObjType
Level
_Level
Merge
Combine
Modified
Modified
Modified
Last_x0020_Modified
Modified By
Editor
Name
FileLeafRef
Name
LinkFilenameNoMenu
Name
LinkFilename
Name
BaseName
Order
Order
owshiddenversion
owshiddenversion
Path
FileDirRef
ProgId
ProgId
Property Bag
MetaInfo
Relink
RepairDocument
ScopeId
ScopeId
Select
SelectTitle
Select
SelectFilename
Server Relative URL
ServerUrl
Shared File Index
_SharedFileIndex
Source Name (Converted Document)
ParentLeafName
Source Url
_SourceUrl
Source Version (Converted Document)
ParentVersionString
Template Link
TemplateUrl
Title
Title
Type
DocIcon
UI Version
_UIVersion
Unique Id
UniqueId
URL Path
FileRef
Version
_UIVersionString
Virus Status
VirusStatus
Workflow Instance ID
WorkflowInstanceID
Workflow Version
WorkflowVersion
In my case, I’m interested in the Name field. I wouldn’t have guessed that it would be mapped to FileLeafRef.

USEFUL REFERENCE FOR SHAREPOINT INTERNAL FIELDS NAME
There are few common and important sharepoint internal fields name which will help to access data in your Web application.
Document Library Fields:
Display Name             Internal Name
Name                                  FileLeafRef
Name                                  LinkFilenameNoMenu
Name                                  LinkFilename
Name                                  BaseName
URL Path                          FileRef
Path                                    FileDirRef
File Size                             File_x0020_Size
File Type                           File_x0020_Type
Source Url                       _SourceUrl
Type                                  DocIcon
Custom List Fields:
Display Name                Internal Name
Name                                     FileLeafRef
Title                                        Title
Order                                     Order
HTML File Type                 HTML_x0020_File_x0020_Type
URL Path                              FileRef
Created                                 Created_x0020_Date
File Size                                 File_x0020_Size
File Type                               File_x0020_Type
File Name                              BaseName
Created By                            Author
Modified By                          Editor

References:
1-http://blog.softartisans.com/2009/12/08/list-of-internal-names-for-sharepoint-fields/
2-https://dipansaha.wordpress.com/2012/07/23/useful-reference-for-sharepoint-internal-fields-name/



No comments:

Post a Comment