Creating, changing and deleting content

Created by Webnodes

To create content you call the “NewContent<>()” method on the WAFSession object:

var article = WAFContext.Session.NewContent<ArticleBase>();
article.Name = "My new article";
article.UpdateChanges();

 

The first statement instantiate a new content object of type “ArticleBase”, but it does not cause any database changes. Once the content is created you can set its properties. In this example we set the Name property. The last statement calls the UpdateChanges() method. This will insert the content to the database. If you make further changes to the same article object you must call UpdateChanges() again:

var article = WAFContext.Session.NewContent<ArticleBase>();
article.Name = "My new article";
article.UpdateChanges();
article.Name = "A different name";
article.Ingress = "This is the article ingress text. ";
article.UpdateChanges();

 

This time UpdateChanges() will cause a database update statement.

If you want to create several contents at the same time and related them to each other you should make sure that the contents you relate to are already inserted before up call the UpdateChanges() method, like this:

var article = WAFContext.Session.NewContent<ArticleBase>();
article.Parent.Set(article.SiteId);
article.Name = "My first article";
article.UpdateChanges(); 

var subArticle = WAFContext.Session.NewContent<ArticleBase>();
subArticle.Name = "My second article";
subArticle.Parent.Set(article);
subArticle.UpdateChanges();

 

Do not do it like this, where the first article is not present in the database when the “subArticle” is inserted:

/* DO NOT DO LIKE THIS*/

var article = WAFContext.Session.NewContent<ArticleBase>();
article.Parent.Set(article.SiteId);
article.Name = "My first article"; 

var subArticle = WAFContext.Session.NewContent<ArticleBase>();
subArticle.Name = "My second article";
subArticle.Parent.Set(article);
subArticle.UpdateChanges();
article.UpdateChanges();

 

This will cause an exception.

To delete a content call the DeleteNode method:

WAFContext.Session.DeleteNode(article.NodeId);

 

This will put the content in the wastebin. To delete it permanently call the overloaded method:

WAFContext.Session.DeleteNode(article.NodeId, true);

 

This method can be called on content that are already in the waste bin or not.

You can also delete a series of contents in with one statement:

WAFContext.Session.DeleteNode(AqlArticleBase.AuthorId == WAFContext.Session.UserId);

 

This will delete all articles created by the user in the current session to the waste bin. The statement will also delete all contents that inherit from ArticleBase.

Inner contents are created by calling the AddNew() method on the inner content relation:

var article = WAFRuntime.SystemSession.NewContent<ArticleBase>();
var innerFile = article.Files.AddNew<InnerFile>();
innerFile.File.SetFile(WAFContext.PathFromRootToAppFolder + "1.jpg", false);
var innerFile2 = article.Files.AddNew<InnerFile>();
innerFile2.File.SetFile(WAFContext.PathFromRootToAppFolder + "2.jpg", false);
article.UpdateChanges();

 

In the above examples two inner contents of type InnerFile is created and added to the inner content property “.Files” on article. The changes are persisted to the database by calling the “.UpdateChanges()” method on the content base class the inner content belongs to.