How to Access Sitecore_Web Database Tables using Sitecore API - sitecore

Hi i m new to Sitecore. I want to know how to access any table values from Sitecore Web Database through Sitecore API as i want to fill dropdownlist from a table inside sitecore_web database.How can we use Sitecore Queries to access data from Sitecore_Web Database?

I don't suggest you to access directly Sitecore_Web Database.What do you want to do exactly ?
Sitecore Api has a lot of classes, methods for accesing sitecore items but you don't access directly databases .
Maybe this link will help you.
You need to create some items to fill your dropdown list.
An item is a record in a database. Items are the basic building block of a Sitecore site. An item may represent any kind of information, e.g. a piece of content, a media file, a layout etc.
Items always have a name and and ID that uniquely identifies the item within the database. Items have a template that defines which fields the item contains. An item represent a single version of piece of content is a single language.
An item can be retrieved from a database using Items.
An item may have a number of subitems or children. These child items can be accessed through the Children property. The resulting items are checked for security and workflow before being returned. So while an item may have subitems, the current user may be denied access to them. The Parent property define the single parent item of this item.
An item represents a single version in a single language of a piece of content. The language of the item can be obtained from the Language property, while the version is available from the Version property.
The item must be in Editing state before the name or any field values can be changed. If not, an exception is raised. To enter the Editing state use the BeginEdit method and to end it, use the EndEdit method. The EditContext class can be used as a shortcut to BeginEdit/EndEdit.
I get this from Sitecore Api Documentation

As sitecore climber stated, you would not access the Sitecore database directly - this is not something which is supported by Sitecore. Typically you would achieve this through the Sitecore API using the following steps:
retrieve a node from the Sitecore tree which contains child items representing your dropdown items
retrieve the child items
databind your dropdown list to the list of child items
In an ascx, you could have something like this:
<asp:DropDownList ID="exampleDropDown" runat="server"/>
and in the code-behind:
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
var item = Sitecore.Context.Database.GetItem("/sitecore/content/SiteData/StuffForADropDownList");
if (item != null)
{
exampleDropDown.DataTextField = "Text";
exampleDropDown.DataValueField = "Value";
exampleDropDown.DataSource = from i in item.Children.AsEnumerable<Sitecore.Data.Items.Item>()
select new
{
Text = i["Text"],
Value = i.ID.ToString()
};
exampleDropDown.DataBind();
}
}
}
This example assumes that you have an item at /sitecore/content/SiteData/StuffForADropDownList with some child items, which each have a field called Text.

Default in Sitecore you will NEVER work with real database objects other then objects filled using the Sitecore API. Sitecore API will provide you with data from your Sitecore back-end.
If you want to fill a dropdown with a list of items as datasource try something like this:
var items = Sitecore.Context.Item.GetChildren().toList();
yourdropdown.Datasource = items;
yourdropdown.Databind();
Obviously setting your Datatext and Datevalue key correctly, but that's standard .Net.
In Sitecore, never directly get data out of your database and use the Sitecore API.

Please use the built in services to access the sitecore items. It provides the option to select the database in the Get Item method.
You can access the sitecore service with the below URL:
{Your Site}/sitecore/shell/webservice/service.asmx

Related

Using a bucket as a datasource for a droplink/tree field

I'm trying to enable content editors to select an item that resides in a bucket in a droplink field but I'm unable to find a field type/datasource that enables this.
I need to allow the user to select a single item (so not a multilist), the items are in a bucket as the number of items may be huge and the search api would be most helpful to the editors.
Is there a field or datasource query that will enable a lookup field to select a single bucketed item?
The simplest solution is to use a Sitecore multilist with search field.
First you need to set the source of your field to display items within your bucket of a specific template(s).
Example: StartSearchLocation={11111111-1111-1111-1111-111111111111}&Filter=+_templatename:sample item
Here is an article describing how to set the source of your field: Sitecore 7 field types
If you need to limit the selection to one item then you need to also apply some regex. To achieve this you need to enable standards values in the view tab so you can alter the data section.
In the data section add the following regex: ^({[^}]+}\|?){0,1}$ and add some validation text.
Example:
This article provides additional infromation:Limit selected items on Sitecore multilist field
Just in case anyone else comes across this as I did, you can also use a query in the source field to filter the items in a droplink.
query:/sitecore/content/Home/YourBucket//*[##templateid='{your-template-guid}']
You can also use ##templatename='Your Template Name'
Keep in mind that unless your bucketed items aren't numerous (for some reason), the suggested answer is probably better since it provides search, and will not create a massive dropdown list of items.
I made some custom fields for this very purpose: https://github.com/Barsonax/SitecoreSearchFields
It gives you the same rich search interface you normally get when searching in buckets.

How to filter items that display in a Sitecore Treelist?

I have the following content structure in Sitecore:
Home
Products
A-E
A Sample Product A
B Sample Product B
F-J
K-O
L Sample Product L
P-T
U-Z
In addition each Product has a checkbox field called "Active". I would like to have a Treelist that lets the user select one or more products - but they should only be allowed to select Products where the Active checkbox is checked. Is this possible?
It sounds like you can achieve what you need using Sitecore query. Check out the documentation for exact syntax rules.
Not all field types support Sitecore query though, so you will need to use a field type that does (Multilist). Or you could implement a custom TreeList field type that works with query.
You can also use the properties IncludeTemplatesForDisplay or IncludeTemplatesForSelection to keep your TreeList fields clean.
IncludeTemplatesForDisplay makes certain only those templates will show up in the TreeList.
IncludeTemplatesForSelection allows you to define which templates you can actually select in the TreeList field.
In your case, you could set IncludeTemplatesForDisplay the template of your Products item, then the A-E etc. items, and also the actual Products template. If you then select IncludeTemplatesForSelection you can make sure the content editors can still only select the actual product item.
Other parameters are ExcludeTemplatesForSelection, DatabaseName, ExcludeTemplatesForDisplay, IncludeItemsForDisplay, ExcludeItemsForDisplay and AllowMultipleSelection.
Do keep in mind that won't allow selections where a checkbox is ticked, but you could consider creating a custom field based on a TreeList field, which has the additional property which checks whether the Active field is set.
I'm not sure that there is a way out of the box to set the datasource of a treelist (or any list based field, for that matter) to a set of Items and filter by the value of their fields.
How about thinking about it differently.
What if you had a mirror of the products section of the tree somewhere else in your tree but, instead of having all products in this mirrored section, you'd have only the products that are active. The items in this mirrored section would have a drop-link field that maps to the original product in the original section of the tree. To keep this list up-to-date, you could use a custom action when the original product is saved. When a product is saved, if the Active checkbox is checked, create an item of type Mirrored Product (for example) in the Mirrored Products section of the tree and set the drop-link to the original product. When a product is saved, if the Active checkbox is not checked, find the Mirrored Product that maps to this product being saved and delete it.
Finally, the datasource for your treelist would be the root of these Mirror Products section of the tree, not the original products themselves. This way, the options for your treelist would always only ever items that map to products whose Active checkbox was checked.
One thing to keep in mind: when you delete a Mirrored Product, if it's referenced by a treelist, that reference will remain; you'll need to have your code check for null before trying to use a treelist item.

Sharepoint: Establish related records when new item being added to list

What is the best way to implement a parent-child hierachy between items in a list? The requirement is as follows
-> there is a list item with a particular ID
-> that particular ID could have related records.
-> all those related records should show up when viewing the list item
When the user adds a new item to list using a form, they should have the option to choose whether the new item being added has another related item or not.
The primary question here is what would be the best way to establish this hierarchy in the list? Also, all the items would be in the same list so how
would we link the list to itself? Or, what would be the best way to implement this? I can figure out several approaches to handle this within the confines of the
sharepoint server object model; however, this needs to be done primarily within the UI of Sharepoint itself (webparts, features, etc. may be used). However,
I am not sure how to IMPLEMENT THIS WITHIN THE SHAREPOINT CONTAINER ITSELF.
Any ideas/insights would be greatly appreciated.
Thanks
You can create a look up column in list with a parent list as itself and allow multiple selections. That way you will be able to check multiple parent IDs for a specific list item.
To show related list items, you can customize details view page of the list in sharepoint designer and add list view web part or dataview with appropriate filters based on the current item.

Filter external list fields from client

BACKGROUND:
We’re developing a custom application which access SharePoint through the Client Object Model and this application need to access ECT (external content type) lists defined in SharePoint using the OM (Object Model). This application is a product that should be usable with most SharePoint installations and configuration and cannot have prior knowledge of External Lists.
When there are no filters set up for the ECT, SharePoint returns all the available items in the list (given the number is below the threshold). The moment we define a filter for this ECT, SharePoint return only the items after this filter is applied (probably correct behaviour from SP).
PROBLEM:
We need to be able to search this ECT list (non-filtered) based on text entered by a user in a search box. At the moment there seem to be no way to change the filter SharePoint applied when returning the values to the calling object.
I.e. I have 10 items in my ECT list (1,2,3…10). Each Item has 3 columns (ID, Name, Description). After setting up a filter for the ECT list, SharePoint return items 2, 3 & 6 when I ask SharePoint for a list of items.
No the user does a search the matches the description of item 7. How can I search/filter the list to return the item that match my search query?
I’ve been running in circles trying to solve this, but nothing seem to work. I tried setting the CAML query as well as the LoadQuery as defined in both http://pholpar.wordpress.com/2011/02/09/how-to-query-external-lists-on-the-client-side-using-caml/ and http://msdn.microsoft.com/en-us/library/ff464384.aspx but nothing seem to work.
Even I had this problem. Let me tell you there is no way of doing this.
Even in Server object model, there was an option to change the filters of the default view but then the SPList will return 0 items once the filter is applied.
The funny part is once your code runs & you open the list in SP UI, you can see the actual modified list. But the same cannot be queries in the object model.
Looks strange. I guess if you modify the SPList (ECT based) in this instance, then you can only get results in the next instance (like in next page refresh.....)

Sitecore: Get child items of parent item when the items are in publish restricted period

I am trying to get the children items of a Item.,of which some of the child items applied publish restrictions and there end date had expired.
I want to collect all the child items, whether it's end expired or not.
when i debugged my code i got only those items which have end date in time.
I have used following code
var childItems=item.GetChildren();
the child items collection is collecting only those child items which have there end date in time.
is there any method to collect all the items??
I have asked in sitecore forum, they asked to get child items from master database, but that approach also not working. i have tried following code also.
Sitecore.Data.Database master = Sitecore.Configuration.Factory.GetDatabase("master");
Item parentNode= master.GetItem("ItemNAme");
var childItems=parentNode.GetChildren();
The childitems is only collecting those child items whose end date is not expired
Please suggest me some solution for this
Thanks in advance
-Vaibhav
What you said about going to the master database is correct, as the master contains all versions of all pieces of content, not just publicly published content. I would wrap your code in a SecurityDisabler() in case that is somehow interfering with getting the right items.
using(new SecurityDisabler()) {
Sitecore.Data.Database master = Sitecore.Configuration.Factory.GetDatabase("master");
Item parentNode= master.GetItem("ItemNAme");
var childItems=parentNode.GetChildren();
}
The reason you find this problem is not related to security.
When IsPreview is true (cookie based) and Filtering on the site config is allowed you will not get Items with publishing restrictions. This functionality is from the implementation of the PageEditor Preview where you need to browse the master database as if published items where not visible.
See the answers on Sitecore Publishable flag makes it impossible to GetItem() from Master database