3

How to show Friendly Date in SharePoint using Javascript or using Server side code

 

JAVASCRIPT

You can use following javascript code in order to show Date field value in Friendly format


var yourDate = new Date();

var friendlyDate = SP.DateTimeUtil.SPRelativeDateTime.getRelativeDateTimeString(yourDate , true, SP.DateTimeUtil.SPCalendarType.none, false)

console.log(friendlyDate);

 

Note that you need to add reference of “SP.dateTimeUtil.js” js in your page

<script type="text/javascript" src="/_layouts/15/SP.dateTimeUtil.js></script>

Following is the description about method argument:
1. Your date (Date ISO8601)
2. Specify whether want to include tiem or not(IncludeTime)
3. Calendar type (CalendarType)
4. Specify whether want to adjust time with server(AdjustToServersNowTime)
————————————————————————–
SERVER SIDE


DateTime date = DateTime.Now;
string friendlyDate = SPRelativeDateTime.GetRelativeDateString(web, date.ToUniversalTime(), date); 

I Hope it will same someone’s time.
 

Advertisements
1

Upload Document using c# code to Share-Point library

Hello everyone,

I was facing the following exception.So i had searched and got the solution.

Exception:
Only String, int, and DateTime data types can be used as the value in Properties while adding list item in document library.

I am including following concept with this blog.

1) how to upload document in to SharePoint library from the other library.
2) pass the document metadata using hash table.
3) How to update custom field of SharePoint library with uploaded document.
4) Limitations of Files.Add() methods with hash table.

Key Concept :
What is hash Table?

  • The Hashtable class represents a collection of key-and-value pairs that are
  • organized based on the hash code of the key.
  • It uses the key to access the elements in the collection.
  • A hash table is used when you need to access elements by using key, and you can identify a useful key value.
  • Each item in the hash table has a key/value pair.
  • The key is used to access the items in the collection.

Requirements:
1)How can I upload document in to the Document library from the other document library and update Document library meta data in share point?
2)How can I update document library custom field with user type and Date time type Using c# code?
Solution:
Let’s take document library:
1) UserInformation(Destination Library)
Custom Fields : 1) DocUpdateDate: DateTime 2)DocAddedBy: User
2) BioData(Source library)

 

//Get the Source library
 SPList BioData = elevatedWeb.Lists.Cast().FirstOrDefault(l => l.id == listID);
 if (bioData!=null)
 {
 spListItem bioDataItem = bioData.GetItemById(itemID);
 if(bioDataItem!=null)
 {
 // get file from the item which you want to upload the destination.
 SpFile objFile = Item.File;
 string fileName = objFile.Name;
 //Getting latest published version file content
 byte[] fileContent = null;
 if (objFile.UIVersionLabel.Contains(".0"))
 {
 fileContent = file.OpenBinary();
 }
 else
 {
 string versionString = string.Format("{0}.0", objFile.MajorVersion.ToString());
 SPFileVersion version = objFile.Versions.GetVersionFromLabel(versionString);
 fileContent = version.OpenBinary();
 }
 // Now open the Destination library where you want to upload document.
 SpList UserInformation = elevatedWeb.Lists.Cast().FirstOrDefault(l => l.ID == listID);
 if(UserInformation!=null)
 {
 // create a hastable to stored the Document metadata.
 HashTable properties = new HashTable();
 // Add metadata key and value of the document.
 properties.Add("Key1","Value1");
 properties.Add("Key2","Value2");
 properties.Add("Key3","Value3");
 // you can number of properties as per the business requirement.
 // now upload the document in to the library.
 // to upload the file ,open the object of the SpFile.
 elevatedWeb.AllowUnsafeUpdates = true;
 SpFile uploadFile = UserInformation.RootFolder.Files.Add(fileName, fileContent, properties);
 }

 

 

Limitations of Files.Add() methods with hash table.
If I add a column of type Currency then the code will break and it won’t add the list item to the library at all.Because the function list.RootFolder.Files.Add() will not accept the properties other than the 3 types mentioned,through HashTable
This is the problem with the function. To avoid that below is the code i used and this problem went away.

 

// create a SPListItem object from the objFile.
spListItem objListItem = uploadFile.Item;
{
if(objListItem!=null)
{
objListItem[“DocUpdateDate”] = DateTime.Now;
objListItem[“DocAddedBy”]=elevatedWeb.currentUser;
// update list item
objListItem.update();
}
uploadFile.update();
}
elevatedWeb.AllowUnsafeUpdates = false;
}

 

Finally, updating the list item and updating the file to reflect the changes to the document library list items.

Please feel free to post your ideas, comments and question here.

Happy Coding 🙂