Duplicate files in SharePoint CSOM - file

I am trying to duplicate files in a document library, with a different GUID/ name in SharePoint using CSOM. This needs to copy all existing metadata.
I am currently uploading files to a document library, each file has a specific set of metadata when it gets uploaded, one of these is a GUID generated by the web application (C# / CSOM). What I need to do is when a copy button is selected in the application any associated file(s) are duplicated and given the new GUID and name, but all other information should be the same.
Thanks in advance.

Get the list item for the file, create a new list item, iterate over the properties of the existing item and copy it to the new item, save the item and use the SaveBinaryDirect of the File object inside the List item to copy it to the new item, then update the GUID to a new one

Related

2sxc Hybrid How to access and update a multi-entity field to add new one

Using Dnn v9.10.2 and 2sxc v13.2
I have a content type with an Entity-type field that holds multiple items, and I need to add a new one to the list from code. I believe that I need to call App.Data.Update(EntityId, Dictionary<string, object>) where the dictionary key is the field name and the value should be a list of EntityGuids, and the list should include all of the existing entities as well. Is that correct? If so how do I access the existing list of entities?
When trying to access the multi-entity field it shows its type as DynamicObject and not a list. I tried several ways of accessing the contents of the field from code including wrapping it in AsList() to use in a foreach loop, but the foreach never runs through an iteration. Is there a different type I need to cast it to inside of the AsList?

How to bind uploaded file with ADAM to entity field correctly in 2sxc module?

I need to create entity object programmatically and fill it with data. One field needs to be of a file type. So I managed to create entity without file, upload file in ADAM using this sample of code. However it seems that I didn't bind it as it is binded when file is uploaded manually.
When a file is uploaded to entity field manually, you can see content like file:421 .../asdf.docx. However when I repeat code sample from link above, field contains that file available to choose and already uploaded, but field value is null. IFile.Url seems to write correct data via App.Data.Update method, but no id is displayed in admin panel.
Dictionary<string, object> fileDict = new Dictionary<string, object>(StringComparer.OrdinalIgnoreCase) {
{ "File", file.Url }
}; // file is ToSic.Sxc.Adam.IFile, returned by SaveInAdam
App.Data.Update(entityObj.EntityId, fileDict); // entityObj is ToSic.Eav.Data.IEntity, returned by App.Data.Create
I wonder if that's going to have some bad conscequences if it has no binding like manual upload and how to do that correctly?
To add files, you actually add them to a field (so that the UI can then offer them as if they were added by a normal user). Check out https://docs.2sxc.org/api/dot-net/ToSic.Sxc.Dnn.Web.IDynamicWebApi.html#ToSic_Sxc_Dnn_Web_IDynamicWebApi_SaveInAdam_System_String_System_IO_Stream_System_String_System_String_System_Nullable_System_Guid__System_String_System_String_
BTW: Best check out MobiusForms to see how it's done.
And I forgot to mention: here the docs https://docs.2sxc.org/web-api/custom-web-api-dotnet-saveinadam.html
To further explain:
SaveInAdam will place the file in the ADAM folder of the item. Since it could be that many files are added, it's just assuming that the field will be the library type - which doesn't store anything in the entity, it just automatically finds the folder that belongs to the field.
If you wish to not use the library feature but actually just the single-field with link, then you must also save the term like "file:74" into the value of the field.

Send file to server through iText PDF?

I'm dynamically creating some PDF files through my application, using iText, and I need to use several components (TextField, CheckBox, RadioButtons etc), and then submit the values to server. But, one of the requirements says that the user needs to be able to select and send files along with the other values. I did not find a specific component to this, and so I'm asking for some help with this situation.
Is there a way to create some kind of Input File, File Chooser, etc, and attach it on the generated PDF file? And then send this selected file to server?
Thanks
This is explained in the official documentation, more specifically in the example FdfServlet of chapter 9 of my book. However, in this example, we add a file selection field to an existing PDF, so I've made you an example that explains how to create such a field when creating a document from scratch: FileSelectionExample
A file selection field is created just like any other text field, except that you have to set a flag using the setOptions() method. If you want a file chooser to appear, you also have to add a JavaScript action:
TextField file = new TextField(writer, new Rectangle(36, 788, 559, 806), "myfile");
file.setOptions(TextField.FILE_SELECTION);
PdfFormField upload = file.getTextField();
upload.setAdditionalActions(PdfName.U, PdfAction.javaScript(
"this.getField('myfile').browseForFileToSubmit();", writer));
writer.addAnnotation(upload);
In the full example, I also added a second field and after selecting the file with the browseForFileToSubmit() method, I set the focus to that other field. I'm doing this because the file name only becomes visible in the file selection field after that field loses focus.

Associating an uploaded File (id) with another table

I'm using Filestore to make uploads in a form.
It's a great implementation but I've a doubt about associating that file uploaded with a register in a table.
When I run the filestore.001.sql, it add 4 tables in Database (filestore_image, filestore_file, filestore_volume, filestore_extension).
Then in anoter table I have:
id
field1
idfile
I was thinking in how to do that association when I submit the form.
Maybe a dsql() and then selecting the last inserted file, but I don't know if this is the best option.
Thanks
Alejandro
When you add a image field to a form, it will return you ID (or list of uploaded file id's) on submission. So theoretically user can upload image and never submit form, hence it wouldn't be associated.
You can have the following table:
id
name
picture_id
and define picture as type image, there should be examples on the site - it will automatically receive ID from the filestore_file table.
There is another way you can do and it's exactly how Filestore_Image is being built. You can make your own model, and link it with necessary image and use that for your image field. Since it's your model, you can now control what happens before inserting, you can override beforeInsert and afterInsert events to build the proper associations.
Both methods have their own uses, pick the one which you like.

Custom Button to copy data from Opportunity into a related custom object

I have a custom object that is used for product setup that is mapped to an opportunity. It's a one to many relationship - one opportunity maps to many setup objects, but one setup object is only mapped to one opportunity.
Opportunity has some setup fields that need to act as defaults for the related custom object. Unfortunately, I cannot just specify them in a formula - getting an error.
What I would like to do is have a custom button that would allow user to click and copy all of the related setup fields from the opportunity into the custom setup object and then edit them as needed.
Any pointers or sample code are greatly appreciated!
You can achieve this with a custom button on the related list for your custom object on the opportunity detail page.
All of the fields on a standard Salesforce new/edit screen have id's associated with them. You can specify values for fields by using these ids to set GET parameters on your URL. For example if the id on the name field on your opportunity is 'opp3', the following URL will populate the name field on your new opportunity page:
https://na2.salesforce.com/006/e?opp3=Hello+World
You would have to change na2 to the correct server for your org.
The new record page URL contains the 3 character id prefix for your particular object and then '/e'. 006 is the prefix for opportunities. You will have to attempt to create a new record to see what the 3 characters are for your custom object.
You will have to capture the id's of the fields you want to populate on your custom object. You can do this by viewing the source of the new record page. For custom fields these id's will take the form of a Salesforce Id (eg. 00N40000002QhEV).
Create a new list button on your custom object and set the behavior to without header and sidebar and set the source to URL. Build up your URL with id=value pairs separated by '&' using the id you got from the page source and the insert field functionality to select the opportunity fields your wish to add in. You should end up with something like this:
/a0U/e?00N40000002QhEV={!Opportunity.Name}&00N40000002QhEW={!Opportunity.StageName}
a0U should be replaced by the correct prefix for your custom object. Then add your button to the related list for your custom object under opportunity.

Resources