Skip to content

Commit

Permalink
Merge pull request #6 from ramarao9/bugfix/5-entitysetname
Browse files Browse the repository at this point in the history
CollectionName uses EntitySetName property
  • Loading branch information
ramarao9 authored May 5, 2020
2 parents a3d8751 + 3b842de commit 44dde88
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 13 deletions.
2 changes: 1 addition & 1 deletion AttachmentDragandDrop/Other/Solution.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<!-- Description of Cds Publisher in language code -->
<Description description="https://github.com/ramarao9/AttachmentUploader" languagecode="1033" />
</Descriptions>
<Version>1.0.0.2</Version>
<Version>1.0.0.3</Version>
<!-- Solution Package Type: Unmanaged(0)/Managed(1)/Both(2)-->
<Managed>2</Managed>
<Publisher>
Expand Down
7 changes: 4 additions & 3 deletions AttachmentUpload/AttachmentUploader.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { faSpinner, faRecordVinyl } from '@fortawesome/free-solid-svg-icons'
export interface UploadProps {
id: string;
entityName:string;
entitySetName:string;
controlToRefresh:string|null;
uploadIcon:string;
context: ComponentFramework.Context<IInputs>;
Expand Down Expand Up @@ -38,7 +39,7 @@ export const AttachmentUploader: React.FC<UploadProps> = (uploadProps: UploadPro
reader.onerror = error => reject(error);
});

const uploadFileToRecord = async (id: string, entity: string,
const uploadFileToRecord = async (id: string, entity: string,entitySetName:string,
fileInfo: FileInfo,context: ComponentFramework.Context<IInputs>)=>{

let isActivityMimeAttachment = (entity.toLowerCase() === "email" || entity.toLowerCase() === "appointment");
Expand All @@ -48,7 +49,7 @@ export const AttachmentUploader: React.FC<UploadProps> = (uploadProps: UploadPro
attachmentRecord["body"] = fileInfo.body;
}
else {
attachmentRecord[`objectid_${entity}@odata.bind`] = `/${entity}s(${id})`;
attachmentRecord[`objectid_${entity}@odata.bind`] = `/${entitySetName}(${id})`;
attachmentRecord["documentbody"] = fileInfo.body;
}

Expand Down Expand Up @@ -88,7 +89,7 @@ export const AttachmentUploader: React.FC<UploadProps> = (uploadProps: UploadPro
entityName = currentPageContext.entityTypeName;
}

await uploadFileToRecord(entityId,entityName,fileInfo,uploadProps.context);
await uploadFileToRecord(entityId,entityName,uploadProps.entitySetName, fileInfo,uploadProps.context);
}
}
catch(e){
Expand Down
2 changes: 1 addition & 1 deletion AttachmentUpload/ControlManifest.Input.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8" ?>
<manifest>
<control namespace="vij" constructor="AttachmentUpload" version="1.0.1" display-name-key="AttachmentUpload"
<control namespace="vij" constructor="AttachmentUpload" version="1.0.6" display-name-key="AttachmentUpload"
description-key="Upload attachments easily using drag and drop" control-type="standard">
<type-group name="flds">
<type>SingleLine.Text</type>
Expand Down
42 changes: 34 additions & 8 deletions AttachmentUpload/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { IInputs, IOutputs } from "./generated/ManifestTypes";
import { AttachmentUploader, UploadProps } from './AttachmentUploader';


interface EntityRef {
interface EntityRef {
id: string,
entityName: string
}
Expand All @@ -16,6 +16,7 @@ export class AttachmentUpload implements ComponentFramework.StandardControl<IInp
private uploadProps: UploadProps = {
id: "",
entityName: "",
entitySetName: "",
controlToRefresh: "",
uploadIcon: "",
context: this._context
Expand Down Expand Up @@ -63,12 +64,17 @@ export class AttachmentUpload implements ComponentFramework.StandardControl<IInp
private getEntityReference(context: ComponentFramework.Context<IInputs>): EntityRef | undefined {
let currentPageContext = context as any;
currentPageContext = currentPageContext ? currentPageContext["page"] : undefined;
if (currentPageContext && currentPageContext.entityId && currentPageContext.entityId !== "") {
var entityRef: EntityRef = { id: currentPageContext.entityId, entityName: currentPageContext.entityTypeName };
return entityRef;
var entityRef: EntityRef = { id: "", entityName: "" };
if (currentPageContext) {
if (currentPageContext.entityTypeName) {
entityRef.entityName = currentPageContext.entityTypeName;
}
if (currentPageContext.entityId && currentPageContext.entityId !== "") {
entityRef.id = currentPageContext.entityId;
}
}

return undefined;
return entityRef;

}

Expand All @@ -80,13 +86,34 @@ export class AttachmentUpload implements ComponentFramework.StandardControl<IInp
this.uploadProps.context = context;

let entityRef = this.getEntityReference(context);
if (entityRef) {
if (entityRef && entityRef.id!=="") {
this.uploadProps.id = entityRef.id;
this.uploadProps.entityName = entityRef.entityName;
}
this.uploadProps.controlToRefresh = context.parameters.ControlNameForRefresh.raw;
this.uploadProps.uploadIcon = this.getImageBase64();//when initially a new record tha's transitioning to an existing record, so the UI is now being updated to enable the content

if (this.uploadProps.entitySetName === "") {
this.retrieveEntitySetNameAndRender(context, this.uploadProps.entityName);
}
else {
this.renderComponent();
}


}

private retrieveEntitySetNameAndRender(context: ComponentFramework.Context<IInputs>, entityName: string) {
var thisRef = this;
context.utils.getEntityMetadata(entityName).then(function (response) {
thisRef.uploadProps.entitySetName = response.EntitySetName;
thisRef.renderComponent();
},
function (errorResponse: any) {
console.log(`Error occurred while retrieving the entity metadata. ${errorResponse}`);
});
}

private renderComponent() {
ReactDOM.render(
React.createElement(
AttachmentUploader,
Expand All @@ -95,7 +122,6 @@ export class AttachmentUpload implements ComponentFramework.StandardControl<IInp
this.attachmentUploaderContainer
);
}

/**
* It is called by the framework prior to a control receiving new data.
* @returns an object based on nomenclature defined in manifest, expecting object[s] for property marked as “bound” or “output”
Expand Down

0 comments on commit 44dde88

Please sign in to comment.