Public editing of a Content Part

Topics: General
Apr 1, 2012 at 9:25 AM

Is there a way to edit a part from the public side using PartDriver.cs? I want user (without admin permission) can insert and edit Parts...

The only way I know is to implements my own edit view without using drivers...

Developer
Apr 1, 2012 at 10:24 AM
Edited Apr 1, 2012 at 10:32 AM

You could enable anonymous users to edit anything you like from the public site by creating your own controller and view. From your controller, use the IContentManager.BuildEditor method to build a shape and render that shape from within your view by invoking @Display() (passing in the shape created from the controller).

Sample:

Controller:

        public ActionResult Edit(int contentId)
        {
            // Load the content item to edit
            var myContent = _contentManager.Get(contentId);

            // Build a shape (essentially by invoking all content part drivers' Editor method)
            var editor = _contentManager.BuildEditor(myContent);
            
            // Casting to avoid invalid (under medium trust) reflection over the protected View method and force a static invocation.
            return View((object)editor);         
        }          

        [HttpPost]
        public ActionResult Edit(int contentId, FormCollection formCollection)
        {
             // Load the content to update
             var myContent = _contentManager.Get(contentId);

             // Update the content item (essentially invokes the overloaded version of the content drivers' Editor method)
             var model = _contentManager.UpdateEditor(myContent, this);

             // If any validation error ocurred, cancel the DB transaction and display the view
             if(!ModelState.IsValid)
             {
                 _orchardServices.TransactionManager.Cancel(); 

                // Casting to avoid invalid (under medium trust) reflection over the protected View method and force a static invocation.
                return View((object) model);
            }

            // Show a notification to the user that the change has been made
            _orchardServices.Notifier.Information(T("Your {0} has been saved.", myContent.TypeDefinition.DisplayName));

            // Redirect to whatever makes sense to you
            return RedirectToAction("Edit"); 
        }

View:

@using (Html.BeginFormAntiForgeryPost()) 
{ 
   @Html.ValidationSummary()
   @Display(Model) 
}

 

To see a working example, checkout Orchard's core Contents module and its AdminController & Views.

 

Apr 1, 2012 at 3:32 PM

Ok thank you! :-)

Another newbie question: in the edit/details view of a ContentType I want display editors of single ContentPart in a grid. Now all editor are shown in a list...what I want is manage the position of single editors in the Content not only before or after other parts but also left and right..

Example: I have a ContentType TeamType (that represent a football team) with two ContentPart :

1- TeamPart with some information (Name, Number of Player, ...)

2- a Picture (mediapickerfield) with the picture of the team

Now wath I want is to display the Picture at left side and information at right side. But, generally, I want to display information like a grid rather than a list..

How can I do?

Developer
Apr 3, 2012 at 8:48 PM

I see. So basically you want to have a customized content editor, for which you can specify into which zones certain shapes (the part editors) are to be rendered.

I think the key thing to understand is that all editors create shapes, which are placed into specific zones. This placement is configurable via Placement.info as well as programmable via IShapeTableProvider.

So you might be able to create a content template that renders specific (local) zones. You configure the shapes to be rendered into those zones. I think you'll be able to figure it out by checking out all of these posts: http://weblogs.asp.net/bleroy/archive/tags/Orchard/default.aspx. Especially the posts that talk about shapes.

Let us know when you have something!