Creating list items
Creating a list item using Native API
When creating a list item through the Native API, you must perform the following:
-
Get the parent list.
First, you must get the parent list for the list item.
For more information about querying lists, see Querying lists.
For more information about finding specific lists, see Finding lists.
-
Create new list item.
After you get the parent list, you must create new list item.
-
Set the parent.
To set the parent list for the list item, you use the Parent property of the list item.
-
Set the required properties.
When creating a new list item, it is recommended to set at least the following properties:
-
Title
-
Content
-
UrlName
-
DateCreated
You can also set any other properties in this step.
-
Save the list item.
Save all changes that you have made to the list item.
-
Publish the list item.
Finally, you publish the list item using the workflow manager.
The following code creates a list item with the specified ID, Parent, Title and Content through the Native API.
NOTE: In the code example below the ID argument is assigned to the master version of the list item.
public void CreateListItemNativeAPI(Guid listItemId, Guid parentListId, string title, string content)
{
ListsManager listManager = ListsManager.GetManager();
// Get the parent list
List parent = listManager.GetLists().Where(l => l.Id == parentListId).FirstOrDefault();
if (parent != null)
{
// Create the list item
ListItem listItem = listManager.CreateListItem(listItemId);
// Set the parent
listItem.Parent = parent;
// Set the list item properties
listItem.Title = title;
listItem.Content = content;
listItem.UrlName = Regex.Replace(title.ToLower(), @"[^\w\-\!\$\'\(\)\=\@\d_]+", "-");
listItem.DateCreated = DateTime.Now;
// Save the changes
listManager.SaveChanges();
// Publish
var bag = new Dictionary<string, string>();
bag.Add("ContentType", typeof(ListItem).FullName);
WorkflowManager.MessageWorkflow(listItemId, typeof(ListItem), null, "Publish", false, bag);
}
}
First, you initialize the ListsManager. You get the parent list using the GetLists method.
For more information about querying lists, see Querying lists.
For more information about finding specific lists, see Finding lists.
You create the list item using the CreateListItem method of the ListsManager class. Note that you can create a list item with either predefined or auto-generated ID depending on which overload of the method you use. The ID argument is assigned to the ID of the master version of the item. Then, you set the parent list and the properties of the list item. It is recommended to set at least the following properties: Title, Content, UrlName and DateCreated. Then, you save the changes. Finally, to publish the list item in live state, you call theMessageWorkflow method of the WorkflowManager class and pass the required parameters.
Creating a list item using Fluent API
When creating a list item through the Fluent API, you must perform the following:
-
Check whether the parent list exists.
First, you must check whether the specified parent list exists.
-
Get the parent list.
If it exists, you must get the parent list.
For more information about querying lists, see Querying lists.
For more information about finding specific lists, see Finding lists.
-
Create new list item.
After you get the parent list, you must create new list item.
-
Set the required properties.
When creating a new list item, it is recommended to set at least the following properties:
-
Title
-
Content
-
UrlName
-
DateCreated
You can also set any other properties in this step.
-
Save the list item.
Save all changes that you have made to the list item.
-
Publish the list item.
Finally, you publish the list item using the workflow manager.
The following code creates a list item with the specified Parent, Title and Content through the Fluent API.
public Guid CreateListItemFluentAPI(Guid parentListId, string title, string content)
{
int count = 0;
Guid id = Guid.Empty;
// Check whether the parent list exists
App.WorkWith().Lists().Where(l => l.Id == parentListId).Count(out count);
if (count > 0)
{
// Get the parent and create the list item
App.WorkWith().List(parentListId).CreateListItem().Do(listItem =>
{
// Get the Id of the list item
id = listItem.Id;
// Set the list item properties
listItem.Title = title;
listItem.Content = content;
listItem.Urls.Clear();
listItem.UrlName = Regex.Replace(title.ToLower(), @"[^\w\-\!\$\'\(\)\=\@\d_]+", "-");
listItem.DateCreated = DateTime.Now;
}).SaveChanges();
// Publish
var bag = new Dictionary<string, string>();
bag.Add("ContentType", typeof(ListItem).FullName);
WorkflowManager.MessageWorkflow(id, typeof(ListItem), null, "Publish", false, bag);
}
return id;
}
First, you check whether the specified parent list exists using the plural facade of the list. If it exists, you get the list using the singular facade.
For more information about querying lists, see Querying lists.
For more information about finding specific lists, see Finding lists.
Then, you create the list item using the CreateListItem method of the facade. Then, you set the properties of the list item by calling the Domethod of the facade. It is recommended to set at least the following properties: Title, Content, UrlName and DateCreated. Then, you save the changes. To publish the list item in live state, you call the MessageWorkflow method of the WorkflowManager class and pass the required parameters. Finally, you return the Id of the list item.
See Also