0

I have added a button to the ribbon for a specific content type on a list (parent list). This is the only content type on the parent list. When an item in the parent list is selected and then the user clicks the button a child item (using newform.aspx) is to be created that is related to the selected parent item.

How is it possible that I can pass values from the selected parent item (e.g. ID and Category, etc.) to the NewForm.aspx for the child item on click of ribbon button?

1

Use tokens in your custom action definition to pass values to the new form. More on this here: https://msdn.microsoft.com/en-us/library/office/ff458385%28v=office.15%29.aspx

Add another custom action with code behind to handle the new form OnLoad event:

<CustomAction Id="NewFormCustomActionId"
                Location="NewFormToolbar"
                ControlAssembly="$SharePoint.Project.AssemblyFullName$"
                ControlClass="CustomActions.YourCodeBehindNamespace"
                RegistrationType="ContentType"
                RegistrationId="0x010020CA30C66D23475D926915EB818F52B905">
</CustomAction> 

Handle the OnLoad event and get the values from your query string. If you need to apply default values to the new form, use SPContext.Current.FormContext.FieldControlCollection.

I'd suggest you use {SelectedItemId} token and with that you will be able to get the parent item from code behind in the newform.aspx:

var list = SPContext.Current.List;
var item = list.GetItemById(SelectedItemIdFromQueryString);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.