How to upload a "Image only from client side to SharePoint Custom List" using Rest Api ? not in Document Library and not in picture Library - rest

I'm new to SharePoint & REST API. So kindly help me with Step by Step Instructions.
I have one custom list called "Attachment" within this custom list I have one Attachment column named as "Attach" which is created with the data type "Hyperlink or Picture".
Using Rest API I want to add the Image which is selected by the end user in client side (browser) to SharePoint Custom list named as "Attachment" under the column "Attach".
Help me with the working Code Samples.
Note: I'm trying this in visual studio 2017 as a SharePoint Hosted App.
This is my Default.aspx page code.
<div class="form-group">
<label>Attachments</label>
<input type="file" class="form-control" id="attch"/>
</div>
"I expect when the user selected any image using this control, I need to upload and store that image to custom List" not in document library , not in picture gallery. Only in Custom List.
Kindly Give me a Visual Studio Solution File (or) Give me a Clear Working code For Default.aspx and App.js with the Script.
Thanks in Advance!!!

The "Hyperlink or Picture" field only store the link of the image.
If you only want to store the image in custom list, we need store the images as attachments. I suggest you store the images into a document library or picture library, then use a "Hyperlink or Picture" to store the link of the images.
To upload image as attachment in list items using REST API, the following articles for your reference.
Working with files attached to list items by using REST
Uploading Attachments to SharePoint Lists Using REST

you need to create a document library instead and upload the image to it,
you can try this js to upload the image,
function _arrayBufferToBase64(buffer) {
var binary = ''
var bytes = new Uint8Array(buffer)
var len = bytes.byteLength;
for (var i = 0; i < len; i++) {
binary += String.fromCharCode(bytes[i])
}
return binary;
}
function uploadfile(name, content) {
var createitem = new SP.RequestExecutor(appweburl);
createitem.executeAsync({
url: appweburl + "/_api/web/GetFolderByServerRelativeUrl('/sites/Tile of web/app title/Lists/list name/foldername')/Files/Add(url='" + name + "',overwrite=true)",
method: "POST",
binaryStringRequestBody: true,
body: content,
success: function (e) {
alert('done');
},
error: function () { alert("Error"); },
state: "Update"
});
}

Related

Dropdown on change open a modal with data

I have a Vendor dropdown in my rails app. I am trying to fetch some data using ajax and currently the returned data is simply displayed in a div on the same page.
I am wondering if it is possible to pass the vendor id to the modal and it then display add the records associated with the vendor after making the ajax call?
here is my current coffeescript code to retrieve the data and display in the div.
$ ->
if $('body.bills.new').length > 0
$('body.bills.new #bill_vendor_id').change ->
$.ajax
url: '/purchase_orders/?vendor_id='+ $('body.bills.new #bill_vendor_id option:selected').val()
success: (data) ->
alert data
$('#data').html data
Of course it is!
You can for example add the class of the dropdown you want to show in the #bill_vendor_id data.
For example:
<input type="text" id="bill_vendor_id" data-dropdown-class="something" ... />
And in the js you find the class and use it to hit the dropdown:
...
$.ajax
url: '/purchase_orders/?vendor_id='+ $('body.bills.new #bill_vendor_id option:selected').val()
success: (data) ->
div_class = $(this).data('dropdown-class')
$('.' + div_class).html data
EDIT
Okay. Yes, it depends. Do you want to change all the html or append some html?
You can either use .html data or .append data
And remember to show the modal, if you're not already doing this.

AEM Forms (PDF Type) File Attachments

I am trying to implement file attachment to my AEM Form. The idea is to embed the attachment to the PDF; Upon submission of the PDF, the backend system will be able to extract the attached file from the PDF.
I can't seem to find any example on how I can go about doing it, which make me wonder is it a limitation. However, on adobe site below, it is actually stated "Support for file attachments" for all AEM form types.
https://helpx.adobe.com/aem-forms/6/introduction-aem-forms.html
Please advice! Thanks!
You can upload, preview, and submit attachments with HTML5 forms. By default, the attachment support is disabled. To enable the attachment support:
1- Create a custom profile with mutiselect string property mfAttachmentOptions.
2- In the custom profile, specify properties fileSizeLimit, multiSelect, and buttonText to configure options of the file attachment widget. As required, you can also specify more custom properties.
3- In the custom profile, use the following configurations:
multiSelect -> true or false (true by default)
fileSizeLimit -> value_in_mb (say 5) (2 MBs by default)
buttonText -> Button text for pop-up window ("Attach" by default)
accept -> file types to accept ("audio/, video/, image/, text/, .pdf" by default)
4- Use the metadata editor to select the custom profile that you have created above for HTML 5 forms.
5- Render your form template with custom profile and the attachments icon would appear on the forms toolbar.
6- Click the attachment icon, an attachment selection dialog box appears. Browse and select the attachment and click Attach.
When attachments are enabled, HTML5 form submits multipart data. The mutli-part submission data has two parts dataXml and attachments.
Reference: https://helpx.adobe.com/aem-forms/6-1/html5-forms/enabling-attachments-html5-form.html
Below is the code for "Add Attachment" button.
var oDoc = event.target;
var doccount = parseInt(colhead.attcount.rawValue);
colhead.attcount.rawValue = doccount + 1;
Trusted_ImportDataObject(oDoc, doccount);
var oAttachment = oDoc.getDataObject("attachment_" + doccount);
var DOsize = oAttachment.size;
var DOmimeType = oAttachment.MIMEType;
if (DOmimeType == null) DOmimeType = "reject";
if ((DOsize > 1100000) || (DOmimeType.indexOf("image/") != 0)) {
xfa.host.messageBox("Attachemtns must be an image and smaller than 1 Megabyte");
oDoc.removeDataObject("attachment_" + doccount);
} else {
var attachPath = oAttachment.path;
AttachFileName.rawValue = attachPath;
AttachMIMEType.rawValue = oAttachment.MIMEType;
AttachHref.rawValue = oAttachment.name;
}
Do note that Reader Extension is needed to enable file attachment. Else you will be getting this error:
NotAllowedError: Security settings prevent access to this property or method.
Doc.importDataObject:6:XFA:CTR[0]:Page6[0]:part6[0]:body[0]:attachitem[0]:AddAttachment[0]:click

Liferay6.0:Rendering DL portlet URL after clicking on custom TreeView portlet

I am using 6.0.6 CE to develop a requirement where a page has two portlets. One at left and other at right hand side.
1. Custom portlet which loads list of folders in a Tree structure for a given community. Here I am using jQuery zTree where doView get me the list of folders in JSON array.
http://www.ztree.me/v3/demo.php#_102
Document Library portlet
Every node folder has URL of document library such as:
http://yourdomain.com/web/oracle/home/-/document_library/view/11614
When I click any of it, reloads the entire page and DL portlet shows children of the given folder id 11614.
But because of this left hand side tree structure gets collapsed.
I wanted to retain the user selection on left side. For this I just thought of rendering only document library portlet on clicking on the folder but I am not able to achieve it thru any of the IPC.
Any suggestions would be appreciated.
Thanks
Prasad
Currently I am following this approach:
During page load I am getting the folderId from URL and in doView(), I am passing it as renderRequest param.
long selectedFolderId = 0;
String friendlyUrlValue = PortalUtil.getCurrentURL(renderRequest);
String[] urlArray = friendlyUrlValue.split("/");
String secondLastword = urlArray[urlArray.length - 2];
if (urlArray.length > 0) {
if (secondLastword.equalsIgnoreCase("view")) {
selectedFolderId = Long.valueOf(urlArray[urlArray.length - 1]);
}
}
JSONObject jSONObject = JSONFactoryUtil.createJSONObject();
createJson(themeDisplay.getScopeGroupId(), 0, jSONObject, renderRequest, fridendlyUrl);
if(selectedFolderId>0){
renderRequest.setAttribute("selectedFId", String.valueOf(selectedFolderId));
}
Refer this post https://www.liferay.com/community/forums/-/message_boards/message/47300920
There should be another approach to achieve reloading of only document Library portlet instead of entire page reload. Please let me know if there is any better approach for this.

Adobe CQ5.5-How to display Page Thumbnail using API

I have configured image for my page using sidekick >Page properties>Images tab. Now I want to fetch my this page image(thumbnail) in one of my jsp. Can someone give me pointers or code snippet for api class and method that I can use to achieve this.
Thanks,
Rajeev
I would suggest using the default image component as an example - /libs/foundation/components/image.
If you're putting your code into a component for your specific page type though, your code should be something like this:
if (currentNode.hasNode("image")) {
String imagePath = currentNode.getNode("image").getPath();
Resource imageRes = resourceResolver.getResource(imagePath);
image = new Image(imageRes);
image.loadStyleData(currentStyle);
image.setSelector(".img");
if (!currentDesign.equals(resourceDesign)) {
image.setSuffix(currentDesign.getId());
}
image.draw(out);
}
Keep in mind though, even though you set an image, it does NOT mean it will show up - if you're using the default page dialog for page properties, it will only show a broken image. That's because there is a bug in CQ where the sling:resourceType property of the image doesn't get set, and thus it won't show up. This is because the .img selector that gets put on the image doesn't know what to do, unless it get's pointed to a resource type with a definition for the .img selector, so it can properly render the image.
I've uploaded a package that you can use as a hotfix for the issue with the default /libs/foundation/components/page component dialog, so that it will actually set the resource type when you upload an image. You can find/download the package from my Google Drive
Hopefully that helps. Let me know if you need more help.
EDIT
If you're trying to get the page properties image from one page on another page, you just need to use a resource resolver. You should have one available to you in CQ, so this would essentially be the code:
Resource imageRes = resourceResolver.getResource(pathFromYourDialog);
Image image = new Image(imageRes);
The rest would be the same - you're just giving it a different path to start from.
I think Nicholaus was more on point with his EDIT answer to your immediate need. If the user is providing you a path to the thumbnail via the dialog (i.e. a DAM image).
You can simply create the image, or if it has DAM information you can load it as a DAM Asset and pull the necessary information.
Image image = new Image();
Resource imageResource = resourceResolver.getResource(imageUrl);
Asset imageAsset = imageResource.adaptTo(Asset.class);
Map<String, Object> valueMap = imageAsset.getMetadata();
long width = Long.parseLong(valueMap.get("tiff:ImageWidth").toString());
long height = Long.parseLong(valueMap.get("tiff:ImageLength").toString());
Object titleObject = valueMap.get("tiff:ImageTitle");
String title = (titleObject == null) ? null : titleObject.toString();
if (title != null)
{
image.setTitle(title);
}
image.setWidth(width);
image.setHeight(height);
image.setUrl(imageUrl);
This is a little long hand for what Nicholaus had suggested, the Image class will create itself based off the Resource you pass it. (actually upvoted Nicholaus for that, have some optimizations we can make).
Another, simpler option would be to just use the src that the user passes through, in the event that all you're doing is setting a thumbnail. I'm guessing you could be safe in doing something like:
in java:
String thumbSrc = properties.get("thumbSrc", "defaultThumbnail.path");
if (!thumbSrc.isEmpty())
{
pageContext("thumbSrc", thumbSrc);
}
in jsp:
<img alt="thumbnail" src="${thumbSrc}"/>
or if you don't want to do anything in the java you could just do something like
<c:if test="${not empty properties.thumbSrc}">
<img alt="thumbnail" src="${properties.thumbSrc}"/>
</c:if>
In order to get the same result as the first part in just jsp, you'd need to wrap it in a choose, because passing it through some processing before sending to view makes it easier to set default values.

Get selected FCK file url from pop-up window

So I've used FCKeditor for TinyMCE. This integrated easily and gave my customers a nice way to upload files while selecting them. To integrate this I used the following code:
function fileBrowserCallBack(field_name, url, type, win) {
var connector = ROOT + "path/to/tiny_mce/filemanager/browser.html?Connector=connectors/php/connector.php";
connector += "&Type=" + type;
browserField = field_name;
browserWin = win;
window.open(connector, "browserWindow", "modal,width=600,height=400");
}
And file_browser_callback: "fileBrowserCallBack" in the TinyMCE call.
Now I want to use this same function to fill a simple input-tag so my users can select an image for a custom background.
Now I created an onClick event on this input field that opens the file-browser. But when I select a file I get the following javascript error:
TypeError: window.top.opener.tinyfck is undefined
So how can I use this same plug-in as a regular file-browser making it return the selected file?
Edit: The actual name of the plug-in I used is TinyFCK
Unfortuanatly, this is not possible. The tinymce image uploader needs the tinymce document structure which is not present when you use another kind of editor.