Remove All Attachments from document xpages - xpages

In my document i have multiple "Body" field which contains Rich-Text data as well as attachment files which is of type (Data Type: MIME Part).
My purpose is to only delete the attachment files from my document. I tried this,
if (document1.getAttachmentList("body").isEmpty()) {
requestScope.alist = "No attachments in body";
} else {
document1.removeAllAttachments("body");
document1.save();
requestScope.alist = "All attachments removed from body";
}
where document1 is my data source name, by this code it works properly.
But, i want to retrieve document by
var doc:NotesDocument = database.getDocumentByUNID(context.getUrlParameter("documentId"));
where it does not works.
I had even tried by this code,
var doc3:NotesDocument = database.getDocumentByUNID(context.getUrlParameter("documentId"));
var item:NotesItem =doc3.getFirstItem("$FILE");
item.remove();
doc3.save();
But, here problem is that it also deleting rich text data from document.
Is there any other solution can help me out.
Thanks in advance.

In the first working code you have a the NotesXSPDocument, but in the second example you have the NotesDocument.
may be it is an idea to convert the NotesDocument to a NotesXSPDocument so you can use the first example
To convert it in SSJS this example may help you
https://openntf.org/XSnippets.nsf/snippet.xsp?id=wrap-notesdocument-into-notesxspdocument

What is the difference between the "NotesXspDocument" and "NotesDocument" class in XPages
https://www-10.lotus.com/ldd/ddwiki.nsf/dx/xpages-notesxspdocument-vs-notesdocument.htm

Related

How to access document attachments from hierarchical transformation

My site has this structure:
Products
category 1
item 1
item 1 attachments
category 2
item 2
item 2 attachments
I have successfully written a hierarchical transformation that shows the data on the page at the top level. I cannot for the life of me figure out how to access the attachments for each document though.
Anyone have any idea?
If you are talking about Group attachments (this is when you have a field in your page type using the Attachments data type) then in order to access the attachments inside your transformation you need to write either a custom macro (when using Text/XML transformation) or custom transformation method. Both can be done very easily. The code itself that gets you the attachments can be like this:
public ObjectQuery<AttachmentInfo> GetAttachmentsFromField(string className, int documentID, string attachmentColumnName)
{
// get class info
var classInfo = new FormInfo(DataClassInfoProvider.GetDataClassInfo(className).ClassFormDefinition);
if (classInfo != null)
{
// get attachment field definition
var attachmentsField = classInfo.GetFormField(attachmentColumnName);
if (attachmentsField != null)
{
// get attachments strored in the field by GUID
var attachments = AttachmentInfoProvider.GetAttachments()
.WhereEquals("3CCC6E6C-56F3-42EB-8385-979973D99C55", attachmentsField.Guid)
.And()
.WhereEquals("AttachmentDocumentID", documentID);
return attachments;
}
}
return null;
}
With this it is very important to take into account that this code introduces several other SQL queries against database and therefore it should be optimized by using caching appropriately.
I suppose you mean unsorted attachments you add in Properties -> Attachments section. In this case yuo can register following control in your transformation:
<%# Register Src="~/CMSInlineControls/DocumentAttachments.ascx" TagName="DocumentAttachments" TagPrefix="cms" %>
And use it like this:
<cms:DocumentAttachments ID="ucDocAttachments" runat="server" TransformationName="cms.root.attachment" Path='<%# Eval("NodeAliasPath") %>' />
I wrote a pretty detailed blog post on this a while back. It describes very similar attributes to Enn's answer but gives great detail on why you do specific things.

Getting document attachments using Kentico API

I created book store site on Kentico i used only their adminstration and display the data from my website using Kentico API's but am strugled in getting attachment files related to specific document i've got document data with no problem using
TreeProvider tree = new TreeProvider(MembershipContext.AuthenticatedUser);
var documents = tree.SelectNodes("CMS.Product");
need also to get related attachment files like book PDFs.. i've tried to use
DocumentAttachment
AttachmentInfo
AttachmentInfoProvider
classes but i couldn't get the data .. I would appreciate if any one help me in that.
Actually am searching about something like GetAttachment().Where("AttachmentFile","Ënglish File")
You can filter the returned attachments based on their values in columns (CMS_Attachment table) by using a code like this:
var attachment = AttachmentInfoProvider.GetAttachments()
.WhereEquals("AttachmentName", "Englishfile")
.And()
.WhereEquals("AttachmentExtension", "jpg")
.TopN(1)
.FirstOrDefault();
if (attachment != null)
{
// attachment was found
}
This code will get one .jpg file where attachment name equals to "EnglishFile"
Solved after using something like
var Attachment = AttachmentInfoProvider.GetAttachments(226, true);
This is from Kentico documentation. This example shows how to add an attachment and modify its metadata. You can ignore that part.You will have to make it generic to work for all examples.
Kentico 9 API Links
// Creates a new instance of the Tree provider
TreeProvider tree = new TreeProvider(MembershipContext.AuthenticatedUser);
// Gets a page
TreeNode page = tree.SelectSingleNode(SiteContext.CurrentSiteName, "/Articles", "en-us");
if (page != null)
{
// Gets an attachment by file name
AttachmentInfo attachment = DocumentHelper.GetAttachment(page, "file.png", tree);
// Edits the attachment's metadata (name, title and description)
attachment.AttachmentName += " - modified";
attachment.AttachmentTitle = "Attachment title";
attachment.AttachmentDescription = "Attachment description.";
// Ensures that the attachment can be updated without supplying its binary data
attachment.AllowPartialUpdate = true;
// Saves the modified attachment into the database
AttachmentInfoProvider.SetAttachmentInfo(attachment);
}

xpages getting the URL after saving a new created doc

I'm looking to get the URLlink for a document. For the existing documents ( which are already saved and when I open them - I get the url with the UNID ) , it's OK, my problem is when I want to get the URL for a new created document - i don't get the UNID inside the URL. ( only, eg: http://myserver/ournsf/doc.xsp?action=newDocument, without the UNID )
My code is something like this:
if(docSource.isNewNote()){
docSource.save();
}
empbody.appendText(context.getUrl().toString())
Thanks for your time!
context.getUrl() gives you only the current URL. It doesn't contain the documentId because you create a new document in your case.
You are looking for an URL that will open current document with current XPage. You can get it with:
var thisdoc = docSource.getDocument(true);
var url = facesContext.getExternalContext().getRequest().getRequestURL().toString() +
"?action=editDocument&documentId=" + thisdoc.getUniversalID());
You can get the url of a new created document with:
document1.getDocument().getUniversalID()
Here document1 is the datasource name of your document.
If you want to redirect to the new created document after a save, you can use this code in a navigation rule or add the following XML:
<xp:this.navigationRules>
<xp:navigationRule outcome="xsp-success">
<xp:this.viewId><![CDATA[#{javascript:return "XPageName?documentId="+document1.getDocument().getUniversalID()+"&action=editDocument"}]]>
</xp:this.viewId>
</xp:navigationRule>
</xp:this.navigationRules>
Where XPageName is the name of the XPage.
You could use the postSaveDocument event of your datasource to calc the URL from the UNID like
var url = "page.xsp?action=openDocument&documentId="+document1.getDocument().getUniversalID();
document1.setValue("url", url);
document1.save();

Extjs: Overwrite objects in a record ready to save

Hi guys can anyone shed some light on how to do this please.
I have a userRecord object with various sub-objects in including things like their skills-sets, contracts etc.
I get the record by querying the store for the userId as below.
var userRecord = userStore.findRecord('id', userId);
Next I have a variety of forms with checkboxes in a tabpanel relating to each of the user's sub-objects e.g. Skillsets and Contracts. I am trying to overwrite these on the userRecord with an array on checkboxes that have been checked.
var skillsetCheckBoxes = skillsetPanel.query('checkboxfield[checked=true]');
var skillsets = new Array();
Ext.each(skillsetCheckBoxes, function (skillset)
{
console.log(skillset);
skillsets.push(skillset);
});
I have tried to set the userRecord's engineer's skillset object to be the new array:
userRecord.set(('engineer').skillsets, skillsets);
But when I log the record after doing this it is still the same record I retrieved from findRecord() with no edited fields.
Any help much appreciated,
Thanks!
Hard to say without knowing your model's structure, but your line is clearly wrong. You are using ('engineer').skillsets as the key argument for the set method. Since it's not a string but an array, it won't do anything good.
Your line should rather be something like that:
// You should probably test that there is something in there, or the
// next line could crash...
var engineer = userRecord.get('engineer');
if (engineer) {
userRecord.get('engineer').skillsets = skillsets;
}

How to make list attachment mandatory in SharePoint 2007

Anyone knows how to make attachment compulsory to the SharePoint custom list?
We are using SharePoint 2007.
There's nothing in the UI / Admin to do this AFAIK.
This arcticle explains how to achieve it with jQuery Link
Edited to correct my previous answer, which on reflection only provided a part answer. The above link is the correct way to achieve this. It shows the mechanism to query the attachments details on the NewForm, ie: before the list item is created, which is the point at which the mandatory function can be applied.
Add the below function as a script in a "Content Editor Web Part" or directly to the form via SharePoint designer. Obviously, this also requires jQuery.
function PreSaveAction(){
var hasAttachment = false;
//There are more then one fileupload inputs on the form
//and one of them will always be blank
$("input[name^='fileupload']").each(function() {
if ($(this).val() != "") {
hasAttachment = true;
}
});
if (hasAttachment){
return true; //OK to save
}else{
alert('An attachment is required!');
return false; //Prevents user from saving
}
}
Better Create a workflow
which will send a mail to last modified by + cc team lead
"if the current item has attachment = no" send mail

Resources