Showing posts with label Site Definition. Show all posts
Showing posts with label Site Definition. Show all posts

Saturday, June 13, 2009

Adding Documents in document library with feature

After creating document library as feature people are asking how to add document in document library as feature.

Refer:How to add document library with folders and content types through feature/site definition.

So here is another simple solution for that.
Below is the feature file

<Feature
Id="BB964AFE-14E1-4f83-9703-73349007D904"
Title="Doclib with document"
Scope="Web"
Version="1.0.0.0"
Hidden="FALSE"
DefaultResourceFile="core"
xmlns="http://schemas.microsoft.com/sharepoint/"
Description="This Feature contains the doc lib with doc">
<ElementManifests>
<ElementManifest Location="element.xml" />
<ElementFile Location="sample\MyDoc1.doc" />
<ElementFile Location="sample\MyDoc2.doc" />
</ElementManifests>
</Feature>



Please note that we have to define our files that we want to put in document library.

This location is path of file from feature.xml.

Below is element.xml

<Elements
xmlns="http://schemas.microsoft.com/sharepoint/">
<Module Name="withDoc"
Url="MyDoc"
Path="Sample"
RootWebOnly="FALSE">
<File Url="MyDoc1.doc" Type="GhostableInLibrary" />
<File Url="MyDoc2.doc" Type="GhostableInLibrary" />
</Module>
</Elements>


Here is the Description for each element.

Module is the most key element for this thing.

Name: name of the module (any) I have used same as feature name for understanding.

URL: Url of document library from site. If you want document in root level of document library then write only document library name over here. If you want document in some folder of document library then user “<Document Library Name>/<Folder Name>/<Folder Name>”

Path: physical path for document folder (files must have in some folder in feature folder) from feature.xml

RootWebOnly: weather allow this feature only at root level or you want this feature to be at sub site level. False will allow you to use this feature to sub site level also.

<File >
Tag


URL = path of the document after path of module.

Type = GhostableInLibrary : as we want the document to be in document library we have to use this tag.

This way you can add documents in document library.

If you want files to be in different folders then you have to use more than one module tag for diff folders.

Below is element for copying one doc in one library and another doc in another library.

<Elements
xmlns="http://schemas.microsoft.com/sharepoint/">
<Module Name="withDoc"
Url="MyDoc/f1"
Path="Sample"
RootWebOnly="FALSE">
<File Url="MyDoc1.doc" Type="GhostableInLibrary" />
</Module>
<Module Name="withDoc2"
Url="MyDoc/f2"
Path="Sample"
RootWebOnly="FALSE">
<File Url="MyDoc2.doc" Type="GhostableInLibrary" />
</Module>
</Elements>


This step you can use for everything like putting any page in pages library, putting master page in master page library and all that.

Wednesday, August 6, 2008

Issue with update list with list definition

Recently we are facing problem of updating list created by list definition.
We are using site definition/list definition for SharePoint deployment.

Here is the scenario.

Now after deployment we got a change request, like add a new field in one of the list.
These are the step we followed for the change:
1) Create a column in that list (on developer pc).
2) Generate a schema file for that list (which has newly column added).
3) As we all know we need to change first line of newly created schema
because we have to keep our old feature id, old ID (Guid), old
template type etc…
4) Replace schema file in 12/template/feature/<<your feature folder>>
5) Uninstall / install feature.

This trick works if you create a new list with that definition.
But what we found that for some list this is not working.
In old list we got a problem.
We only have a single column which is “Title” available in new/edit list.
Again if we create a new list with this it works perfectly but if you opens old list then you will get only single title column for insertion and updation.

For resolving this problem, there is one another thing that to be taken care.
There is “<ContentTypes>” tag in schema.xml for list definition
What we have observed is that while taking new definition of List, Content type id is also getting changed.
So that what we had done is keep old contain type id in new schema file.
And bingo…..it’s working.

Wednesday, May 28, 2008

How to set custom master page as default master page with site definition in Moss and WSS 3.0?

Changing a default master page for our custom need is not at all good idea and creating a custom master page and use it as default master page is a headache.

Here is the way to create and use your custom master page with site definition.

Go to “C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\TEMPLATE\GLOBAL” where you can find default master page (default.master).

Copy that default.master and pest it with customdefault.master or your own custom name.
(Note: while copying default.master please take care that you must not have already modified It. Use a fresh copy)

Put your custom master page at same location as default.master.

Now go to “12\TEMPLATE\GLOBAL\XML” open “ONET.xml”

At the last you will find module tag which provision “default.master” same way add your entry for custom.master.


<Modules>
<Module Name="DefaultMasterPage" List="116" Url="_catalogs/masterpage" RootWebOnly="FALSE">
<File Url=" customdefault.master" Type="GhostableInLibrary" IgnoreIfAlreadyExists="TRUE" />
<File Url="default.master" Type="GhostableInLibrary" IgnoreIfAlreadyExists="TRUE" />
</Module>
</Modules>



This code will automatically put your custom masterpage into the masterpage gallery when a new site is created.

Now open your onet.xml file from 12/template/SiteTemplates
Change the default masterpage to the new one.
Find <Configuration> tag
Add MasterUrl attribute like MasterUrl=”_catalogs/masterpage/customdefault.master” and if you already have this tag change it with your custom master page.

So it will look like

<Configuration ID="0" Name="YOUR NAME" MasterUrl="_catalogs/masterpage/customdefault.master">



Now the new site created with this definition have this master page as default master page. Click here for more info.

What about already existing site?

For that you need to put master page manually and change master page url for each wab
Here is the code snippet (only for setting master page url of the web):
Note: use this snippet carefully. If you run this snippet and you do not have Master page in Master page gallery for that web you will get master or page not found error.


SPSite spsite = new SPSite("Your site name");
foreach (SPWeb Myweb in spsite.AllWebs)
{
string masterurl = Myweb.ServerRelativeUrl;
if (!masterurl.EndsWith(@"/"))
masterurl = masterurl + @"/";
Myweb.MasterUrl = masterurl + "_catalogs/masterpage/customdefault.master";
Myweb.Update();
}



This kind of snippet you will find in Inside WSS 3.0 book

Tuesday, May 20, 2008

How to add Content Type through Feature?

Hi all,
Here is the way how to create a Content type through feature.

Steps:
1) Create a feature (I hope you know how to create a feature or go to this MSDN link.)
2) In element.xml add following xml tag

<ContentType
ID="0x010100BC4AA99D05D3488e95993BA063E6F6A2"
Name="Name of Content Type"
FeatureId="{E282F40C-1ECD-47d7-9E6A-BD3FBDA8500C}"
Description="Description of Content Type"
Group="Your Own Group"
Version="1">
<DocumentTemplate TargetName="your template.doc" />
</ContentType>
<Module Name="Name of Content Type"
SetupPath="Features\Feature_ContentTypes" Path=""
Url="_cts/Name of Content Type">
<File Url="your template.doc" />
</Module>

3) Explanation of this tag,
ID = new generated content type ID [0x010100<NEW GUID without hyphen>]
FeatureId = feature id used in feature.xml
Group = your group name if group exist then it will create a new group in content type.
DocumentTemplate = name of your document/excel template

4) Module will relate your template to content type.
Name = name of your content type. Same as content type name.
SetupPath = relative path for physical template as we are putting our template in feature folder.
Path= no need to give as we are using SetupPath.
Url = all the content type template wil be saved in _cts folder. You can see this folder in sharepoint designer only. After “_cts/<Name of content type>” shold be same as Module name and ContentType name.
File Url = this will be name of document.


All done.
Only need to install the feature and activate the feature.

You can refer How to add document library with folders and content types through feature/site definition.

Friday, May 16, 2008

How to add document library with folders and content types through feature/site definition.

When creating custom features, definitions one of the things most clients
want is a document library included in the Site Template with a
pre-defined folder structure.


In this post I’m going to narrate how to provision document library with folders form a custom feature.

So here we go.

Open element.xml of your feature it should be something like this.

<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
<ListInstance
FeatureId="00BFEA71-E717-4E80-AA17-D0C71B360101"
TemplateType="101"
Title="My Docs"
Url="My Docs"
Description=""
QuickLaunchUrl="My Docs/Forms/AllItems.aspx" />
</Elements>



Over here we are just creating an instance of a document library.
In SharePoint by default you will find a feature with forlder name “DocumentLibrary” in your 12/template/feature. So we do not need to create whole schema file just like custom list.







Creating a list instance with this
GUID is same as creating a document library from

Site Settings > Create > Document Library.




Description of each property

FeatureId --> Hardcode guid which refers SharePoint Document Library.
TemplateType --> 101 denotes that we want to create Document library. 100 for list and 115
for content type etc...
Title --> My Docs simple title of newly created List.
Url --> My Docs same as Title. You can use different if you want but same will be better to maintain and understand.
Description --> just description non mandatory.
QuickLaunchUrl --> My Docs/Forms/AllItems.aspx Just like /Forms/AllItems.aspx, use you used in url tab.

If you installed feature with this element TAG you will get Document library.

Now here we go for folder.
Add the following code just before the </Elements> tag:

<Module Path="First Folder" Url="My Docs \First Folder” />

Note : The Url in the above tag begins with the url of the list instance.

If you want to add sub folders to the above folder

<Module Path="First Folder" Url="My Docs \First Folder\sub folder of first folder” />


Note : The ListUrl in the above tag begins with the url of the list instance.

If you want to add content type to the folder then you need to add one more tag

<ContentTypeBinding ListUrl="My Docs" ContentTypeId="0x010110" />

Note : you already have a contentype installed with specific ContentTypeId.

My next Post will be: How to create/install Contentype from feature. Comming Soon.

You can create multiple document libraries from one feature.
Just add more than one List instance.
Here is one sample of my element.xml

<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
<!-- Doc lib 1-->
<ListInstance
FeatureId="00BFEA71-E717-4E80-AA17-D0C71B360101"
TemplateType="101"
Title="My Docs"
Url="My Docs"
Description=""
QuickLaunchUrl="My
Docs/Forms/AllItems.aspx" />

<!-- required custom content type for this document library instance -->

<ContentTypeBinding ListUrl="My Docs"
ContentTypeId="0x010100A107D9F002764eea923263A0B3587210" />
<!-- Single folder no sub-folders -->

<Module Path="Administration" Url="My Docs/Administration" />

<!-- A Folder and associated sub-folders -->
<Module Path="Development" Url="My Docs/Development" />
<Module Path="Development" Url="My Docs/Development/Code Review" />
<Module Path="Development" Url="My Docs/Development/PreRequisite" />
<!-- Doc lib 2-->
<ListInstance
FeatureId="00BFEA71-E717-4E80-AA17-D0C71B360101"
TemplateType="101"
Title="Your Docs"
Url="Your Docs"
Description=""
QuickLaunchUrl="Your Docs/Forms/AllItems.aspx" />

<ContentTypeBinding ListUrl="Your Docs" ContentTypeId="0x010100D8B76062F25E42efB17D4CBF9747FB57" />
<Module Path="Business Analyst" Url="Your Docs/Business Analyst" />
<Module Path="Functional Analyst" Url="Your Docs/Functional Analyst" />
<Module Path="Functional Analyst" Url="Your Docs/Functional Analyst/Requirment" />
<Module Path="Functional Analyst" Url="Your Docs/Functional Analyst/change Request" />
</Elements>



and speacial thanks to "bobthebuilder" for make me under
stand all this functionality from his wonderful post @ SHAREPOINT.H@CK.








Share your SharePoint Experiences with us...
As good as the SharePointKings is, we want to make it even better. One of our most valuable sources of input for our Blog Posts comes from ever enthusiastic Visitors/Readers. We welcome every Visitor/Reader to contribute their experiences with SharePoint. It may be in the form of a code stub, snippet, any tips and trick or any crazy thing you have tried with SharePoint.
Send your Articles to sharepointkings@gmail.com with your Profile Summary. We will Post them. The idea is to act as a bridge between you Readers!!!

If anyone would like to have their advertisement posted on this blog, please send us the requirement details to sharepointkings@gmail.com