Sharepoint Document Library Schema.xml Customization - sharepoint

Hi I am trying to add a custom field to the Schema.xml of Document library in sharepoint
here is the code that I took from a blog
In the ID i have to put the guid to do so
do I have to add my own guid or do i have to query the sharepoint database and find the guid and paste it there...
If i have to get it from sharepoint database which database and in what table I will find this information....
any help will be greatly appreciated
Thanks,
srikrishna.

This is now a duplicate post from MSDN forms
http://social.technet.microsoft.com/Forums/en-US/sharepointcustomization/thread/72df8c80-9e58-4c09-b1a6-ddfe1fb96b0a

there is a sure and safe mean :
1 - create a list with the SharePoint UI
2 - add a column to the list within the SharePoint UI
Create an applicative page that gets the schema of the list and you are done :
SPList mylist=SPContext.Current.Web.Lists["myNewList"];
string schema = mylist.SchemaXml;
schema = schema.Replace("<", "<");
schema = schema.Replace(">", ">");
string myAddedColumnSchema = mylist.Fields["MyAddedColumn"].SchemaXml;
myAddedColumnSchema = myAddedColumnSchema .Replace("<", "<");
myAddedColumnSchema = myAddedColumnSchema .Replace(">", ">");
Response.Write(schema);
Response.Write("<br>");
Response.Write("<br>"); Response.Write(myAddedColumnSchema );
Response.Write("<br>");
Response.Write("<br>");
This gives you the schema of the list with the field, and also just the field row in the list schema (of course copy the result in Visual Studio and use "Format Document" because on the web page it is just unreadable.
You cannot use this list schema by copying and pasting it in a SharePoint 2010 list Schema but you can locate the place where put the XAML correponding to your custom field.
After that you have to create a custom Content Type just for your custom field.
With SharePoint 2007 you could put a local content type corresponding to the custom field within the list schema but IT IS OVER. (I think... if someone can do it I will be pleased to be wrong ;-))
Then install the list feature with the new schema, and activate it within a site.
Then install the Content Type feature and activate it.
Then create a list based on the new schema, allow content type gestion for that list and add the new content type.
All the items based on the new content type will be allowed to use the new field.

Related

Orchard - Query Custom Fields in Content Item

I have a Content Type "News" with custom field "NewsDate" (DateTimeField) and "Active" (BooleanField)
Now I'm need to get 3 active atimes order desc by NewsDate
Get all news, make them toList() and from there manipulate the data is not a solution.
P.S. I need to do something like:
var items = contentManager
.Query(Entities[PageType.Press])
.OrderByDescending<CommonPartRecord, DateTime?>(record => record.PublishedUtc)
.Slice(0, 3);
but instead of PublishedUTC use my custom field "NewsDate" and add Active == true, However it is not possible due to Orchard architecture of storing custom data in a separate field as XML data.
UPDATED:
In a nutshell I want to generate from code behind the following Query:
DECLARE #temp as TABLE(id int, xmldata xml)
INSERT #temp VALUES(1,'<Data><News><NewsDate>07/14/2011 11:42:00</NewsDate><Link Title="" DisplayText="" Link="www.example.com" OpenInNewTab="True">www.example.com</Link></News></Data>')
INSERT #temp VALUES(2,'<Data><News><NewsDate>07/11/2011 12:11:00</NewsDate><Link Title="" DisplayText="" Link="www.example.com" OpenInNewTab="True">www.example.com</Link></News></Data>')
INSERT #temp VALUES(3,'<Data><News><NewsDate>02/21/2012 16:56:00</NewsDate><Link Title="" DisplayText="" Link="www.example.com" OpenInNewTab="True">www.example.com</Link><NewsLink></NewsLink></News></Data>')
SELECT
TOP 3 [id],
[xmldata].value('(Data/News/NewsDate)[1]', 'datetime') as NewsDate
FROM #temp
ORDER BY NewsDate DESC
P.S. I looked through the code for DynamicContentQueryTests, however all the examples uses the Part, and in my case Fields are just in the ContentItem:
E.g. News content type contains NewsDate field (datetime field) and some parts as well
Any suggestions are greatly appreciated.
Querying fields is possible since 1.4 through Projector and underlying index tables and new APIs on Content Manager. Your simplest bet actually may be to create a projection.
To get the values of fields that have been attached directly to a Content Item, you need to first look for the Content Part with the same name as the item, which is created by Orchard. So in your case in the Parts list for each News Content Item you'll find a part called "NewsPart", and inside this the Fields property will have your NewsDate and Active fields.
However, like you say Orchard serializes the field values into XML for storage to prevent it having to change the database structure every time you add/remove a field to/from a content type. For this reason, querying and ordering by fields is not recommended because all the serialized data needs to be de-serialized for each Content Item. If you want to be able to do this, the best way is to make your own Content Part with a Content Part Record and use your own table for storage, then you can do this:
contentManager.Query<NewsPart, NewsPartRecord>()...
...and query/sort on whatever values you like.
Bertrand is correct. Check this link : http://www.davidhayden.me/blog/projector-module-in-orchard-cms. You can implement your requirements easily with Projection Module. The link will tell you the steps to do that. If you don't want a widget, you can create a page with your query too. Once the module is enabled, you will see the option to create a Projection Page in the left hand side navigation.
EDIT :
Can't you simply parse the XML? You can query your content type 'News'. Something like -
var contentItems = contentManager.Query<ContentPart>("News").Join<TitlePartRecord>().Join<AutoroutePartRecord>().Join<CommonPartRecord>().List();
Once you have that, you can access the XML data by :
foreach (var item in contentItems)
{
var contentItem = (ContentItem)item.ContentItem;
ContentItemVersionRecord contentItemRecord = contentItem.VersionRecord;
string data = contentItemRecord.Data;
//Call some function here to parse 'data' and store the object in the list.
}
'data' has the information in XML format that you need.
You can parse it and then store the objects in your list that you can order by your custom field.

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

sharepoint lookup field how to programmatically filter to lookup only in current document library

I want the lookup field to be filtered to only display titles from the current document library (meaning the document library that the user is currently in). I cannot specify the document library b/c I don't want to have to define a new lookup column for each new document library...instead I'm hoping to do this as a custom content type that can be used in any document library.
It would ideally be deployed as a feature.
Look into http://msdn.microsoft.com/en-us/library/ms446361.aspx">SharePoint Custom Fields.
http://msdn.microsoft.com/en-us/library/ms446361.aspx
This kind of feature does not exist on SharePoint OTB.
Take look at this thread: http://social.technet.microsoft.com/forums/en-us/sharepointadmin/thread/722EE26B-EC33-4618-8041-FAA92E149DE8. It provides some solutions though.

Add a lookup field to a content type in sharepoint

How can I add a lookup field to a content type in sharepoint using the xml definition? (I'm getting errors).
Things to note:
- The lookup list will exist when the content type is added to the document library.
- The lookup list will always have the same name.
- The lookup list has a space in the name.
This is what I've added to the xml:
<Field ID="{GUID}"
Type="Lookup"
List="$Resources:core,lists_Folder;/List%20Name"
ShowField="Title"
Name="MyLookupFieldName"
DisplayName="MyLookupFieldName"
StaticName="MyLookupFieldName"
Hidden="FALSE"
Required="FALSE"
Sealed="TRUE"
>
When I then programatically add the content type to a document library I get an exception (with no useful information), and the following is logged to the sharepoint log:
08/18/2009 17:13:39.50 w3wp.exe (0x08B8) 0x11B0 Windows SharePoint Services Database 6f8g Unexpected Unexpected query execution failure, error code 8114. Additional error information from SQL Server is included below. "Error converting data type nvarchar to uniqueidentifier." Query text (if available): "{?=call proc_GetListMetaDataAndEventReceivers(?,?,?,?,?,?)}"
Luckily, in SharePoint 2010, you can declaratively do this by setting all required properities as shown in the following working example.
<Field Type="Lookup" DisplayName="Link Type" Description="Represents link type."
Required="TRUE" EnforceUniqueValues="FALSE" List="Lists/Links Types" WebId="~sitecollection"
Overwrite="TRUE" PrependId="TRUE" ShowField="Title" UnlimitedLengthInDocumentLibrary="FALSE"
Group="Research Links Columns" ID="{a15e9fa2-4ea0-41f1-a583-b21d53cf72d3}"
SourceID="{30650f6f-fbb8-4acc-a935-29745f5d3c59}" StaticName="Link_x0020_Type"
Name="Link_x0020_Type" Hidden="FALSE" ReadOnly="FALSE"></Field>
It's important to set WebId to have value of "~sitecollection" and set Overwrite to be TRUE.
More info
The problem is that you need to reference the GUID of the list not its title. As you probably won't know the GUID of the list then you can't do this without executing some custom code afterwards.
Even if you aren't using VSeWSS, the last steps in the post dahlbyk has linked to show you how to do this. Chris O'Brien has gone to the trouble of making a CodePlex project that will help you if you aren't using VSeWSS.
Ok, so I couldn't get the xml definition of a field for a content type to work for me for some reason. I did find out how to do it in code. The solution that worked for me is to not add the Field definition in xml, instead add it in code:
Add the content type to the list (in site definition code, or wherever).
Add a field lookup to the given SPWeb (so the field is a web? field, rather than a site field)
Add a new field link to the list content type.
Update the content type.
For example:
SPContentType myContentType = myWeb.Site.RootWeb.ContentTypes["MyContentType "];
myLib.ContentTypes.Add(myContentType);
myContentType = myLib.ContentTypes["MyContentType "];
myWeb.Fields.AddLookup("MyLookupFieldName", myWeb.Lists["MyLookupListName"].ID, false);
SPFieldLink myFIeldLink = new SPFieldLink(myWeb.Fields["MyLookupFieldName"]);
myContentType.FieldLinks.Add(myFIeldLink);
myContentType.Update();

How to create a lookup column that targets a Doc Lib and uses the 'Name' of the document?

How do you create a lookup column to a Document Library that uses the 'Name' of the document as the lookup value?
I found a blog post that recommends adding another custom field like "FileName" and then using a item reciever to populate the custom field with the value from the Name field but that seems cheesy.
Link to the blog in case people are interested:
http://blogs.msdn.com/pranab/archive/2008/01/08/sharepoint-2007-moss-wss-issue-with-lookup-column-to-doc-lib-name-field.aspx
I've got a bunch of custom document content types that I dont want to clutter with a work around that should really work anyway.
I created a one step workflow to set the title from the name, fired on modify and created. Seems to work and took seconds to create.
One way you can do this (although not the easiest way) is by creating a custom field type that extends the SPFieldLookup class. SharePoint's field editor for Lookup fields purposefully hides any columns types that aren't supported by Lookup fields, but you can create a field editor for your custom field type that shows them.
However, I have created a Lookup column that points to a Name column in a Document Library before, and it probably doesn't work like you'd expect. While the value stored in the lookup column is valid, it doesn't show up right in List view or on the View Properties form.
The solution you posted may actually be the best way to handle this. Lookup fields require some kludges if you want to handle more complex scenarios, but that's because they're not meant to provide the same functionality as a foreign key relationship in a database.
Coding in any form always scares me. So Here's what I did: I simply renamed the Stupid "Title" Field to something else, say "Keywords", since you cant do anything with that field: cant even make it mandatory.
Then I created another Single line field called "Title" and used this field for the Lookups
Well there is a simple solution to that and in might work in some case.
In the nutshell if you make the Title field Mandatory, this will force the user to enter a title. In that manner we can use title field as a lookup field.
Now How to do that?
One you are done create a document library go to the library setting. Select Advance Setting and Select Yes for the option "Allow management of content types?".
Then go back to the Library setting and Under content types select the "Document" Content type. THen Select Title Column and then Select "Required (Must contain information)" and say OK.
Now try uploading a document to this document library. You will see Title field in the form.
Hope this helps
Cheers
Vaqar
You have to add the field as XML with the ShowField as 'FileLeafRef'
var XmlFieldDefinition = "<Field DisplayName='myLookupColumn' Type='LookupMulti' StaticName='myLookupColumn' Name='myLookupColumn' Required='FALSE' List='THE LOOKUP ID HERE' WebId='THE WEB ID HERE' UnlimitedLengthInDocumentLibrary='TRUE' Mult='TRUE' Sortable='FALSE' ShowField='FileLeafRef' />"
Field fld = fieldCollection.AddFieldAsXml(XmlFieldDefinition, true, AddFieldOptions.DefaultValue);
ClientContext.Load(fld);
ClientContext.ExecuteQuery();

Resources