Saturday 29 July 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.
TITLEINTERNAL NAME
Approval Status_ModerationStatus
Approver Comments_ModerationComments
Check In Comment_CheckinComment
Checked Out ToCheckoutUser
Checked Out ToCheckedOutTitle
Checked Out ToLinkCheckedOutTitle
Content TypeContentType
Content Type IDContentTypeId
Copy Source_CopySource
CreatedCreated
CreatedCreated_x0020_Date
Created ByAuthor
Document Created ByCreated_x0020_By
Document Modified ByModified_x0020_By
EditEdit
Edit Menu Table End_EditMenuTableEnd
Edit Menu Table Start_EditMenuTableStart
Effective Permissions MaskPermMask
Encoded Absolute URLEncodedAbsUrl
File SizeFile_x0020_Size
File SizeFileSizeDisplay
File TypeFile_x0020_Type
GUIDGUID
Has Copy Destinations_HasCopyDestinations
Html File Linkxd_ProgID
HTML File TypeHTML_x0020_File_x0020_Type
IDID
ID of the User who has the item Checked OutCheckedOutUserId
Instance IDInstanceID
Is Checked out to localIsCheckedoutToLocal
Is Current Version_IsCurrentVersion
Is Signedxd_Signature
Item TypeFSObjType
Level_Level
MergeCombine
ModifiedModified
ModifiedLast_x0020_Modified
Modified ByEditor
NameFileLeafRef
NameLinkFilenameNoMenu
NameLinkFilename
NameBaseName
OrderOrder
owshiddenversionowshiddenversion
PathFileDirRef
ProgIdProgId
Property BagMetaInfo
RelinkRepairDocument
ScopeIdScopeId
SelectSelectTitle
SelectSelectFilename
Server Relative URLServerUrl
Shared File Index_SharedFileIndex
Source Name (Converted Document)ParentLeafName
Source Url_SourceUrl
Source Version (Converted Document)ParentVersionString
Template LinkTemplateUrl
TitleTitle
TypeDocIcon
UI Version_UIVersion
Unique IdUniqueId
URL PathFileRef
Version_UIVersionString
Virus StatusVirusStatus
Workflow Instance IDWorkflowInstanceID
Workflow VersionWorkflowVersion
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/