How to get all items in a search scope in SharePoint 2010? - sharepoint

I thought I could just do something like this:
SPQuery oQuery = new SPQuery();
oQuery.ViewAttributes = "Scope='MyScope'";
SPListItemCollection collListItems = oList.GetItems(oQuery);
but it is pulling back a bunch of stuff that is not in the scope. Is there a different way I should be doing this?

You are confusing SPQuery, which a list querying class, based on CAML, and the search API. Take a look into FullTextSqlQuery, which is the class you really should work with.

Related

Accessing sharepoint from caml

How can I add javascript to my CAML code?
For example, I want to calculate some rates/dates according to local field on Sharepoint list.
I want to set value of field according to the javascript result.
Any Idea ?
Poli .
You can add JavaScript to your page, not to CAML.
CAML is used to query Sharepoint lists.
The results will be rendered as HTML
Have a look at the rendered HTML and go from there.
You can't really "add" javascript code to a CAML query because CAML queries run on the server where as javascript runs client side.
Say you have a query like this :
SPQuery query = new SPQuery();
query.Query = "<Where><Eq><FieldRef Name='FieldName' /><Value Type='Text'>TestValue</Value></Eq></Where>";
When you run your query :
SPListItemCollection items = list.GetItems(query);
You will end up with your items. This is where you can modify them and run your logic code (in your backend code).
For example :
foreach(SPListItem item in SPListItemCollection)
{
int rate = item["SomeField"].ToString() + item["SomeOtherField"].ToString();
//Do whatever you want with the result
}

Programmatically adding columns to Sharepoint List Views

For a project I am working on, I am trying to do the following thing.
There's a Sharepoint 2010 environment, with a few Custom Lists created in Visual Studio. I am adding some fields to them using background code in the FeatureActivated function in the EventReceiver.
What I am doing there is adding a lookup field to the Sharepoint List, and setting the properties to allow it to lookup values from the other list I am adding to the Sharepoint Site.
However, I can't find a function to add it to one of the views. I've tried modifying the Schema Xml, but I can't seem to find a function to reinsert it to the List, and when using the Xml file from the View, I can't seem to make it work.
Is there an easy way to programatically add a field to a view? This would help me out, since there seems to be no way to do this correctly.
This can also be solved if one could explain my other question I have.
I would like to know how one could make Lookup fields in the Schema XML file. I have a Custom Content Type, and Custom Fields, and I am currently trying to look up the Naam field in the Intermediairs List. (This one is also created when deploying this solution). When searching Google, it seems I have to use either a name / the GUID of a List Instance here, but I don't know the GUID of the List Instance beforehand.
<Field ID="{7CC49D9D-F6F5-4A4A-851F-3152AAAAB158}" Type="Lookup"
List="Intermediairs" Name="IntermediairLookup" DisplayName="Intermediair"
StaticName="IntermediairLookup" Group="Onboarding" ShowField="Naam" />
You should know that this code seems to work:
SPWeb web = null;
SPSite site = null;
if (properties.Feature.Parent is SPWeb)
{
web = properties.Feature.Parent as SPWeb;
site = web.Site;
}
if (properties.Feature.Parent is SPSite)
{
site = properties.Feature.Parent as SPSite;
web = site.RootWeb;
}
web.AllowUnsafeUpdates = true;
SPList changeList = web.Lists.TryGetList("Onboarding");
SPList sourceList = web.Lists.TryGetList("Intermediairs");
if (changeList != null && sourceList != null)
{
changeList.Fields.Delete("IntermediairLookup");
var PrimaryColumnStr = changeList.Fields.AddLookup("Intermediair", sourceList.ID, true);
var PrimaryColumn = changeList.Fields.GetFieldByInternalName(PrimaryColumnStr) as SPFieldLookup;
PrimaryColumn.LookupField = sourceList.Fields["Naam"].InternalName;
PrimaryColumn.Update();
}
But yeah. I can't figure out how to do it in XML form. Anyone has any ideas? A solution to either of the questions would solve my core issue.
Greetings,
Mats
EDIT: Well, the question has now been answered, thanks again!
One thing though. I would really like to know at some point how to do something like this in XML / CAML. Does anyone know how to do that? Anyone who's still reading this thread?
Take a look at SPView.ViewFields
http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.spview.viewfields.aspx

GetListitems results from SharePoint?

The GetListitems web service relies on XML to retrieve data from SharePoint.
Some actual working code is:
var doc = new XmlDocument();
doc.LoadXml("<Document><Query><Where><Contains><FieldRef Name=\"DeliveryStatus\" /><Value Type=\"Text\">Created</Value></Contains></Where></Query><ViewFields /><QueryOptions /></Document>");
XmlNode listQuery = doc.SelectSingleNode("//Query");
XmlNode listViewFields = doc.SelectSingleNode("//ViewFields");
XmlNode listQueryOptions = doc.SelectSingleNode("//QueryOptions");
XmlNode items = wsLists.GetListItems(ListName, string.Empty, listQuery, listViewFields, string.Empty, listQueryOptions, null);
Once XMLNode is populated, is there a tried and tested way to traverse the collection of childnodes?
I know how to do this using .net general techniques, but what concerns me is that there might be some pitfalls I am unaware of. For example, I noticed the firstChild and lastChild are empty during some of my tests. I wonder if this is an exception rather than a rule. Its impossible to know from MSDN documentation, so if any of you guys have experience with this please share.
Thanks in advance
You mean FirstChild and LastChild sometimes are empty? Well, if you have tags like <QueryOptions /> and such, then it means they have no childs. By logic I guess that those properties must be empty then.
Or is there any other problem than that?

problem with SPQuery for whole sitecollection?

HI, Can we use SPQuery to query whole site collection. Means I have a list which is in different site so is it possible to use SPQuery for that or I need to use SPSiteDataQuery.
I know SPSiteDataQuery would work in this case but I don't wanna search all the lists as I know the name of the list and in SPSiteDataquery i can't mention the list name and in my case list name is unique in whole site collection. If not SPQuery whats the best method to query my list . I don't wana use Guid over here....
Any suggestions?
Thanks,
If you know the exact position of the list (i.e. the (sub)web it is in), you can just use
using(SPWeb otherWeb = SPContext.Current.Site.OpenWeb("urlofweblistisin"))
{
SPList yourList = otherWeb.Lists["YourListName"];
SPListItemCollection = yourList.GetItems(yourSPQuery);
}

How do I create a Sharepoint list via a feature receiver and an existing list template

I have a list template in a MOSS List Template Gallery and I need to create a list using this template from a feature receiver. This sounds really easy but I cannot find a way of doing this.
SPWeb has a GetCatalog method that returns an SPList with 1 item - my template - but it is an SPListItem and I need an SPListTemplate. How can I 'convert' the item to the correct type?
Thanks
Use the GetCustomListTemplates method of the SPSite object to get the SPListTemplate object representing your custom template. Then use the SPListCollection.Add method to create a new list from this template. In code this would look something like this:
using (SPSite site = new SPSite("http://server/sites/site"))
using (SPWeb web = site.OpenWeb())
{
SPListTemplateCollection templates = site.GetCustomListTemplates(web);
SPListTemplate template = templates["MyTemplates"];
Guid listId = web.Lists.Add("Title", "Description", template);
}
You have to use the internalname...something like this:
foreach (SPListTemplate template in web.ListTemplates)
{
if (template.InternalName.Equals("MyTemplateName")
{
return template;
}
}
So, we gave up and instead have used a feature receiver to create the list totally from code. ListDefs are a complete PITA - C# is a much more logical way to create lists, plus you get the added benefit of being able to code upgrades to lists.
Thanks all.
Read my answer to this question. With that you should get a result from GetCustomListTemplates instead of just an empty list.

Resources