Page 1 of 2

Get a count of attachments

Posted: 18 Dec 2014
by Dan_C
Hi Dmitry,

Any suggestion on how I can get a count of the attachments added so far? I have a requirement for a minimum number of attachments.

Thanks!

Re: Get a count of attachments

Posted: 18 Dec 2014
by Dan_C
The other option I'm considering is using the Related Items feature as each attachment relates to an item.

http://spform.com/office-365/crea ... office-365

I was reading this article and I just want to confirm with you...

Can I pass the current item's id into a field in another list (child)... and thus cause the new item to be related to the parent?

The related items section on the parent form would always be empty until a new item was created which linked the two via a field in the child called Parent_ID with the parent item's ID field.

Hopefully I'm explaining that in a clear way. I'd want to add related items and associate them with the current parent item by default.

Re: Get a count of attachments

Posted: 19 Dec 2014
by Dmitry Kozlov
Hi,

Yes, you can use the Related items control. In the post you've mentioned above, I described how to prepopulate a lookup field in a child form via JavaScipt. Please, note that you cannot use related items in a new form because the parent item is not created yet.

You can still use the attachments field and I can provide you with code that checks whether the user has added a particular number of attachments.

Re: Get a count of attachments

Posted: 19 Dec 2014
by Dan_C
Yes - I came across that issue.

I think it's better for my scenario that I get a count of the number of attachments. What do you suggest?

Re: Get a count of attachments

Posted: 22 Dec 2014
by Dmitry Kozlov
Please, place the following code into JS-editor.

Code: Select all

fd.onsubmit(function() {
	if ($('#idAttachmentsTable tr').length < 2) {
		alert('Please, add at least two attachments');
		return false;
	}
	
	return true;
});
It checks whether the current user has added at least two attachments before the submission.

Re: Get a count of attachments

Posted: 22 Dec 2014
by Dan_C
Hi Dmitry,

Works great. Thank you.

How about getting a count of the number of related items for a similar purpose? I know this question will come up as I add related items to my form.

Re: Get a count of attachments

Posted: 23 Dec 2014
by Dmitry Kozlov
Hi,

It depends on the rendering mode of the related items. Please, specify which one do you use: server or client side?

Re: Get a count of attachments

Posted: 23 Dec 2014
by Dan_C
Not sure yet. I am just adding it in this morning. Could you mention both ways?

Re: Get a count of attachments

Posted: 24 Dec 2014
by Dmitry Kozlov
Please, assign a CSS class to the related items control in Forms Designer e.g. related-items. Next, insert the following code into JS-editor:

Code: Select all

fd.onsubmit(function() {
	if ($('.related-items .ms-listviewtable > tbody > tr').length < 2) {
		alert('Please, add at least two attachments');
		return false;
	}
	
	return true;
});

Re: Get a count of attachments

Posted: 06 Oct 2017
by Katy
Sorry for bringing this old tread up... It is logical to add here than start a new one.

I need to restrict the number of related items: not more than 25, and not permit to save more (it's a registration form)... is there a way to do that?