Pages

Wednesday, June 23, 2010

removeAttachment in XPages

The following is a simple function that will consume a document's url and remove a specified attachment from the same on the fly from Xpages.. I have created the same to help a blog reader who was facing issues with the same.



/******************************************************
@Author : Karthikeyan A
@Created: 23-Jun-2010, Qatar
@Purpose : To delete a specific attachment from a document, provided the attachment
                name and the doc url are provided@Type : Function
@Name : removeAttachment
@Param : targDB:NotesDatabase, The database in which the attachment is present
@Param : doc_Url:String, The url of the document that contains the attachment
@Param : fileName:String, The name of the attachment to be deleted
*******************************************************/
function removeAttachment(targDB,doc_Url,fileName) {
//passing parameters by reference
var docUrl:String= doc_Url;
var targetDB:NotesDatabase=targDB;
var attachmentName:String =fileName;


//deducing the document's unid from the document's url
docUrl=@Left(docUrl,"?");
var docUnid=@RightBack(docUrl,"/");


//setting the handle to the document
var docContext:NotesDocument=targetDB.getDocumentByUNID(docUnid);
if (docContext==null) {
viewScope.CodeError="Either the UNID is invalid or the target db does not contain the doc or both";
return;
}


//getting the handle to the concerned attachment
var embObj:NotesEmbeddedObject=docContext.getAttachment(attachmentName);
if (embObj==null) {
viewScope.CodeError="No attachment is found by the name "+ attachmentName;
return;
}

//remove the attachment
embObj.remove()
//save the document so that the changes gets into effect.. other wise no change will be infered
docContext.save(true,false);
}

Hope this helps :)

No comments:

Post a Comment