How do I query Composite C1 media objects? - c1-cms

Given I have a Guid pointing to an Image. How can I query that image for it's meta data fields (title, description, etc)?
Is there helpers for reading the description of an image?

Something like this works.
string description = null;
using (DataConnection connection = new DataConnection())
{
var mediaFile = connection
.Get<IMediaFile>()
.Where(m => m.Id == pictureId)
.FirstOrDefault();
if(null != mediaFile)
{
description = mediaFile.Description;
}
}

Related

How can I get an item by a string key using web api?

I can't figure out how to hit an endpoint like "api/GetItems/AB123" (AB123 of course being a string) and have it return that item from my data set. I read the docs on the FindAsync() method and it seemed to indicate that it would accept a string by default. Is there something I need to do to 'id' before passing it into FindAsync()? My DB does not have a primary key, if that matters. (I can't change that either, this is legacy data and I have no control over schema)
My db doesn't have a PK ID field. I need to do the next best thing and target a unique string field.
My GET method:
// GET: api/Items/5
[HttpGet("{id}")]
public async Task<ActionResult<Item>> GetItem(string id)
{
var item = await _context.Items.FindAsync(id); // Error happens here: "InvalidCastException: Unable to cast object of type 'System.String' to type 'System.Int64'."
if (item == null)
{
return NotFound();
}
return item;
}
Relevant from my model:
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public string ItemId { get; set; }
Hello and welcome to the Stack Overflow Community!!
Instead of find you could do .SingleAsync() like the below.
You could also do a .Where(x => x.ItemId == id).SingleAsync(). But this is up to you.
// GET: api/Items/5
[HttpGet("{id}")]
public async Task<ActionResult<Item>> GetItem(string id)
{
var item = await _context.Items.SingleAsync(x => x.ItemId == id);
if (item == null)
{
return NotFound();
}
return item;
}
From your error it is obvious that int is expected in FindById method. Can you check the field type in database but from your model I would say that you don't have correct type.
String can't be used as Identity in this way because SQL Server doesn't know how to generate value for that.
You can check this post for more details on that: DatabaseGeneratedOption.Identity not generating an Id
So to conclude, you should check what do you really have in the database to determine is your model wrong.
If that is not the case and you do have a string in the db you should just retrieve item by using SingleAsync method (note that it will throw exception if the id is wrong).
var item = await _context.Items.SingleAsync(e => e.ItemId == id);
If you don't want an exception if the id doesn't exist you can use:
var item = await _context.Items.SingleOrDefaultAsync(e => e.ItemId == id);
which will return null for non existent id.

Upload File (image/audio/video) to FeedItem in chunks

​public static String upload(String folderId, String documentId, String fileName, String base64BlobValue) {
if(documentId == '' || documentId == null) {
Document document = new Document(Name=fileName, FolderId=folderId, Body=EncodingUtil.Base64Decode(base64BlobValue), IsPublic=true);
insert document;
return document.Id;
} else {
Document document = [select Id, Body from Document where Id = :documentId];
update new Document(Id = documentId, Body = EncodingUtil.Base64Decode(EncodingUtil.Base64Encode(document.Body) + base64BlobValue));
return documentId;
}
}
I'm trying to upload files (image/audio/video) to FeedItem in chunks, the above example shows how I'm doing the same while writing to a document. But If try to do the same with FeedItem it says "FeedItem.ContentData is not writeable" so I've tried the following code:
FeedItem imageFeedItem = [select Id, ContentData, RelatedRecordId from FeedItem where Id = :imageFeedItemId];
ContentVersion content = new ContentVersion();
content.versionData = EncodingUtil.Base64Decode(EncodingUtil.Base64Encode(imageFeedItem.ContentData) + base64BlobValue);
content.pathOnClient = fileName;
content.ContentDocumentId = [Select ContentDocumentId from ContentVersion where id=:imageFeedItem.RelatedRecordId].ContentDocumentId;
insert content;
But this creates ContentVersions of incomplete chunks. Any pointers on how can I achieve this more neat.
Thanks
You can try setting IsMajorVersion to false
ContentVersion content = new ContentVersion();
content.versionData = EncodingUtil.Base64Decode(EncodingUtil.Base64Encode(imageFeedItem.ContentData) + base64BlobValue);
content.pathOnClient = fileName;
content.IsMajorVersion = false;
content.ContentDocumentId = [Select ContentDocumentId from ContentVersion where id=:imageFeedItem.RelatedRecordId].ContentDocumentId;
insert content;
I hope this help!

read zip attachment in visualforce page

Hi all I am developing an app on salesforce.
I want to read the content of the file which is inside the zip attachment in visualforce page but without extracting the zip file.
How can I achieve this? Is there any way to do this?
Update for modified question:
Have a look at Handling Office Files and Zip Files in Apex – Part 2 by Andrew Fawcett.
There is a basic Knowledge Article on how to do this with an image that is stored in an Attachment. See How can I Display Base64 Data on page layout?
In this example the AttachmentID is passed via a query string paramemter, but you could look it up however works best for your requirement.
Visualforce page:
<apex:page controller="ViewImage" cache="true">
<img src="data:{!att.ContentType};base64,{!image}" />
</apex:page>
Controller:
public class ViewImage {
public Attachment att {
get {
if (att == null) {
String id = ApexPages.currentPage().getParameters().get('AttachmentID');
att = [SELECT Body, ContentType, Name FROM Attachment WHERE ID = :id];
}
return att;
}
private set;
}
public String image {
get {
return EncodingUtil.Base64Encode(att.body);
}
}
}
Hi all I have achieved this using JSzip library here is my code --
In apex page I have written javascript function --
function viewContent(){
var zip = null;
var zipFileName = null;
var zipFileNames = null;
data = "{!contentAsText}";
zip = new JSZip(data, {base64:true});
zipFileName = 'files.zip';
zipFileNames = [];
for(var zipfileName in zip.files){
zipFileNames.push(zipfileName);
if(zipfileName == 'index.html'){
var file = zip.files[zipfileName];
var data = file.data;
document.getElementById('contentdiv').innerHTML = data;
//var data = JSZipBase64.encode(file.data);
}
}
In controller --
public String contentAsText {get;set;}
List<Attachment> atts = [Select Id, Body from Attachment where name='files.zip' limit 1];
contentAsText = EncodingUtil.base64Encode(atts[0].Body);
This link will help you --
http://andyinthecloud.com/2012/12/09/handling-office-files-and-zip-files-in-apex-part-2/

How to get name of a RavenDB document

My code is:
using (var session = documentStore.OpenSession(databaseName))
{
var list = session.Query<dynamic>("Raven/DocumentsByEntityName").ToArray();
foreach (var item in list)
{
Console.WriteLine(item);
}
}
But it does not give me the name of the document. I want to list all of the documents in single database.
Try something like this, it's a bit more generic and it allows access to the raw documents
using (var session = store.OpenSession())
{
//Issue a dummy query to make sure the indexing has finished
var dummyQuery = session.Query<dynamic>("Raven/DocumentsByEntityName")
.Customize(x => x.WaitForNonStaleResultsAsOfLastWrite())
.ToList();
//First get all the document types, i.e. the different entity names
var docTypes = store.DatabaseCommands.GetTerms("Raven/DocumentsByEntityName", "Tag", "", 128);
foreach (var type in docTypes)
{
Console.WriteLine("\n{0}:", type);
//Might need to do paging here, can only get at most 1024 docs in 1 go!
var docs = store.DatabaseCommands.StartsWith(type, 0, 1024).ToList();
foreach (var doc in docs)
{
Console.WriteLine(" {0}: {1}", doc.Key, doc.ToJson());
}
}
}
modifying Matt waren's code for specified database.
public void DocumentNamesWithMetadata(string databaseName="1")
{
using (var session = documentStore.OpenSession(databaseName))
{
//Issue a dummy query to make sure the indexing has finished
var dummyQuery = session.Query<dynamic>("Raven/DocumentsByEntityName")
.Customize(x => x.WaitForNonStaleResultsAsOfLastWrite())
.ToList();
//First get all the document types, i.e. the different entity names
var docTypes = session.Advanced.DatabaseCommands.GetTerms("Raven/DocumentsByEntityName", "Tag", "", 128);
foreach (var type in docTypes)
{
Console.WriteLine("\n{0}:", type);
//Might need to do paging here, can only get at most 1024 docs in 1 go!
var docs = session.Advanced.DatabaseCommands.StartsWith(type, 0, 1024).ToList();
foreach (var doc in docs)
{
Console.WriteLine(" {0}: {1}", doc.Key, doc.ToJson());
}
}
}
}

url sharepoint list camlquery

I'm trying to collect my url and the description of the url stored in a column of a list from sharepoint and i don't know how to collect the URL value.
This is my code :
var queryResultSaleListItems = clientContext.LoadQuery(listData);
clientContext.ExecuteQuery();
//Read the Data into the Object
var TipsList = from Tips in queryResultSaleListItems
select Tips;
ObservableCollection<Tips> colTips = new ObservableCollection<Tips>();
//Read Every List Item and Display Data into the DataGrid
foreach (SPSClient.ListItem item in TipsList)
{
var tips = new Tips();
tips.TitleTip = item.FieldValues.Values.ElementAt(1).ToString();
tips.App = item.FieldValues.Values.ElementAt(4).ToString();
//should collect the url
tips.URL = item.FieldValues.Values.ElementAt(5).ToString();
//should collect the description of the url
tips.URLdesc = item.FieldValues.Values.ElementAt(5).ToString();
colTips.Add(tips);
}
ListboxTips.DataContext = colTips;
In my expression its >
((Microsoft.SharePoint.Client.FieldUrlValue)(item.FieldValues.Values.ElementAt(5))).Url
((Microsoft.SharePoint.Client.FieldUrlValue)(item.FieldValues.Values.ElementAt(5))).Description
Thanks for your help,
Use FieldUrlValue for getting hyperlink field in Client Object Model.
Use Following Code:
string server = "siteURL";
var ctx = new ClientContext(server);
var web = ctx.Web;
var list = web.Lists.GetByTitle("CustomList");
var listItemCollection = list.GetItems(CamlQuery.CreateAllItemsQuery());
ctx.Load(listItemCollection);
ctx.ExecuteQuery();
foreach (Microsoft.SharePoint.Client.ListItem listItem in listItemCollection)
{
string acturlURL = ((FieldUrlValue)(listItem["URL"])).Url.ToString(); // get the Hyperlink field URL value
string actdesc = ((FieldUrlValue)(listItem["URL"])).Description.ToString(); // get the Hyperlink field Description value
}

Resources