Home
Github
Wiki
Videos
Contact
Sponsor
Using the ContentService
you can use the content service to create new content, save/publish or sort, move, delete, publish, unpublish or do any of the other things the backoffice allows you to do. ## Create content after injecting the `ContentService` into your controller, you can do the following to create new content: ```cs var viewModel = await contentService.Create
(parentId, "en-gb",nodeName,published:true); await contentService.SaveContent(viewModel); ``` you can use `Create
` to first make a new instance of your ViewModel. you could actually create a new instance of your ViewModel directly without using `Create
` but then you'd have to set a bunch of properties yourself rather than letting the create method do it for you. note that if you don't supply the `template` argument to the `Create
` method and you haven't set the Allowed Templates for your ViewModel in the settings section of the backoffice, it will throw an error. if you had set the Allowed Templates, it would be able to pick the first allowed template from the list and set the template to that. so make sure you either provide the template argument or set the Allowed Templates in the settings section. `SaveContent
` is used to create, save and publish your ViewModels. if your ViewModel's `Published` property is set to true, it will publish. ## Editing content if you want to edit existing content, you will first need to get the revision from the database using the Puck Repository. so inject an instance of `I_Puck_Repository` into your controller and get the revision by doing this: ```cs var homepageRevision = repo.CurrentRevision(homepageId, "en-gb"); ``` you now have an instance of `PuckRevision` but `ContentService` saves ViewModels not revisions so next you need to get an instance of your ViewModel: ```cs var homepageViewModel = homepageRevision.ToBaseModel() as Homepage; ``` now you have an instance of your ViewModel, you can edit it: ```cs homepageViewModel.Title = "Welcome to the Homepage"; ``` and then you can save it: ```cs await contentService.SaveContent(homepageViewModel); ``` by default, saving will create a new revision but you can opt to overwrite the current revision if you like, by setting the `makeRevision` argument to false. ## Bulk changes when making bulk changes, calling `SaveContent` can become inefficient because it Commits on the lucene index and also opens a new Lucene Searcher each time. one strategy to deal with this is to pass the `shouldIndex` boolean parameter as `false` to `SaveContent`. but this means you will need to `Index` the items yourself after all the Saves are done. since `SaveContent` returns a list of ViewModels that should be indexed, you can put these in a `List
` and then call `Index` on the content service after all your saves are done. ```cs var toIndex = new List
(); foreach(var vmodel in ViewModels){ //edit view viewmodel here, changing properties etc //save the viewmodel and add the items that should be indexed to the list var saveResult = await contentService.SaveContent(vmodel,shouldIndex:false); toIndex.AddRange(saveResult.ItemsToIndex); } //now bulk index all the viewmodels contentService.Index(toIndex); ``` the above code shows you how you should handle bulk changes. this method is quicker because it only Commits on the Lucene index once and will only need to open a new Searcher once rather than for each item in the ViewModels list. you will need to inject `I_Content_Service` in your controller to get an instance of the content service.