Programmatically adding columns to Sharepoint List Views - sharepoint

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

Related

SharePoint How To Get a ViewName

I need to get data from SharePoint by using web service. I did it, but i don't know how to get data from View.
XmlNode itemNode = spList.GetListItems(url[1],
null,
ndQuery,
ndViewFields,
null,
ndQueryOptions,
null);
Second parameter must be ViewName.
I have found System.Xml.XmlNode ndListView = spList.GetListAndView(url[1], ""); but it return default ViewName ("AllItems") guid. There's more than two views.
Is any idea?
i have solved the problem. I have used List web service but there's another View. So I looked it and found the method i need GetViewCollection, as i remember.
That's all!
You can use GetList method, which returns a Caml List Schema and from which you can extract a description of all view.
GetList method

Adding a ListItem to a list in Sharepoint 2007

I am trying to add an item to a list in Sharepoint. At the moment I am trying to add the item via CAML
I can read the list, and query the list but I have not been able to add to the list. All the examples that I have seen update the list, I would expect that it should be reasonably similar process to add an item.
this is how I am testing it at the moment.
SPLists is a web reference to http:///_vti_bin/lists.asmx
void Test(){
var listService = new SPLists.Lists();
string strBatch ="<Method ID='1' Cmd='New'><Field Name='Title'>Test</Field></Method>";
XmlDocument xmlDoc = new System.Xml.XmlDocument();
System.Xml.XmlElement elBatch = xmlDoc.CreateElement("Batch");
elBatch.SetAttribute("OnError", "Continue");
elBatch.SetAttribute("ListVersion", "1");
elBatch.InnerXml = strBatch;
XmlNode ndReturn = listService.UpdateListItems("TestList",elBatch);
Console.Write(ndReturn.OuterXml);
Console.WriteLine("");
}
someone already asked a similar/same question here on SO but not answered
Edit
This is the error that I get
<Results xmlns="http://schemas.microsoft.com/sharepoint/soap/">
<Result ID="1,New">
<ErrorCode>0x81020026</ErrorCode>
<ErrorText>The list that is referenced here no longer exists.</ErrorText>
</Result>
</Results>
When I setup the web reference pointed it at the correct site and even looked at the list in sharepoint to make sure that it is there.
Looks like you probably need a small addition to your strBatch (use this article as a reference): <Field Name='ID'>New</Field>
Which means you'll have something like:
string strBatch ="<Method ID='1' Cmd='New'><Field Name='ID'>New</Field><Field Name='Title'>Test</Field></Method>";
Also, if you have any required fields on your list, you'll probably have to specify those as well.
This is what I found that solved my problem.
When I set up the Web Reference in visual studio I pointed it to the http://sharepointSite/subweb1/subweb2/_vit_bin/lists.asmx as the reference.
However when I went back and checked today it was pointing to http://sharepointSite/_vit_bin/lists.asmx. manually changing it back to http://sharepointSite/subweb1/subweb2/_vit_bin/lists.asmx in the app.config file made all the difference.
#Kit +1 I also added in you suggestion as well. With your suggetion and what I discovered about the web reference, it worked first time.
I ended up just creating a sub web with only 1 field (Title) just to get it working.

Updating the Internal Name using the Content Database

We currently have a field which has the wrong ID. the IDs and Internal Names of Sharepoint Fields are readonly on the domain model. I was wondering if there is a way to update them even by using the content database.
One sure way is to delete the field and recreate it. but it already has data and there are thousands of pages. I was wondering if there is a way just to update the IDs and Internal Name without doing the dropping and recreation of fields.
Thanks
Even if it may work, don't do it.
It's:
Dangerous, as you may skip dependencies
Not supported
Recreating the field using some script to keep data is safer.
We ran into the same problem. Messing with the DB was not an option for us (and shouldn't be for you either), but you can work around it. Unfortunately, it will require touching each page to update the metadata.
First, create the column like it should be in the list. Then, you can copy the data from the old column to the new column in a variety of ways:
DataSheet view
Programmatically via web services (don't need to have access to the server)
Programmatically via console app (will need to run locally on the server)
Honestly, writing a small console app will be the quickest. For example:
string siteUrl = "http://server/sitecollection/";
string webUrl = "subsite1/subsite2/";
string listName = "Your List Name";
using (SPSite site = new SPSite(siteUrl))
{
using (SPWeb web = site.OpenWeb(webUrl))
{
SPList list = web.Lists[listName];
foreach (SPListItem item in list.Items)
{
item["New_x0020_Column_x0020_Name"] = item["Old_x0020_Column_x0020_Name"];
}
}
}
Also, it would HIGHLY recommend trying this in a DEV environment first. Just do an STSADM restore from your production environment and test your console app. Then, validate your data and delete the old column!
I would not suggest modifying the content database, since it is:
unsupported (if you do it, Microsoft will not help you when you're in real trouble even if you have MS Premier Support)
very complicated to do. You'd have to update a number of tables and you would never be sure if you actually updated all the required things.
What you can try to do - see if the internal name property is actually "read only" or it is defined as "friend" and you cannot call it from a different code assembly. If it's the latter case, you can use .Net reflection to set the property value. See this MSDN documentation link for details.

Display document library setting as a report

I have a sharepoint site which contains site and subsite and document libraries in it. Couple of document library has setting to maintain the versioning of the doc along with the comments.
Now I have requirment where a client wants to see this settings site wise as, under which site how meny doc libraries are there which have versioning enabled...?
I want to show this information as an report.
Do I need to write a custom webPart or code for it ? Or how can I show this information as a report in sharepoint.
Thanks in advance.
Sachin
Versioning information is a property of the SPList class even though only document libraries can use versioning in SharePoint.
How you output this is up to you but here is some quick code to get you started.
Use the SPWeb.GetListsOfTypeMethod(SPBaseType.SPDocumentLibrary) to return an SPListCollection, loop through the list collection checking for the SPList.EnableVersioning property.
//Get your SPWeb whichever way works best
SPListCollection lists = web.GetListsofType(SPDocumentLibrary);
foreach (SPList list in lists)
{
if(list.EnableVersioning = true)
{
// Write to a list or update a count
}
//Output count results or a list of the doc libraries
}
Cheers, CJ

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