Showing posts with label XML. Show all posts
Showing posts with label XML. 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.

Friday, May 30, 2008

How to use custom redirection page in SharePoint for custom navigation?

The requirement:
Create a page in SharePoint which will redirect you to specific location based on inputs.

All we need to reach at our custom page
For that you can use many ways like...
From Hyper link from quick launch.
From Edit Control Block.
From FormToolbar in List Add/Edit/Display mode and all that.
Click here to know how to do that.

What we need to do is that create a new ASPX page which will execute our custom code.

How to create a new aspx page in SharePoint with feature?
Here is a sample:
I will name this feature as “CustomRedirection”.
Here is the content of elements.xml

<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
<Module Name=" CustomRedirection" Path="" Url="">
<File Url=" CustomRedirection.aspx" Type="Ghostable" IgnoreIfAlreadyExists="FALSE" />
</Module>
</Elements>


The Module element works same as Module element in Onet.xml.
This element useful for using files like webpart or custom page.
So this feature includes our CustomRedirection.aspx as part of site page.
Over here we are putting “Ghostable” page (physical aspx) with these feature.

how aspx looks like?
Here is the content of CustomRedirection .aspx page

<%@ Assembly Name="Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Assembly Name=" MyCustomCode, Version=1.0.0.0, Culture=neutral, PublicKeyToken=3f3cc8bff1d91557" %>
<%@ Page Language="C#" MasterPageFile="~masterurl\default.master"
Inherits=" MyCustomCode. CustomRedirection "
EnableViewState="true" EnableSessionState="True" EnableViewStateMac="false" %>

<asp:Content ID="PageTitleInTitleArea" runat="server" contentplaceholderid="PlaceHolderMain" >
Test
</asp:Content>

You can find it out that in this page what we had done is that adding Microsoft.SharePoint assembly and our custom assembly, which contain code we want to executes) then one asp:content with id “ PageTitleInTitleArea” and contentplaceholderid as “PlaceHolderMain”. This content is used by master page.

Now question is
how our custom code executes when this page accessed?
You can see page derivatives in that we are specifying MasterPageFile and we are inheriting our own class (Inherits=" MyCustomCode. CustomRedirection"). So when this page called our custom code in class CustomRedirection.cs will call.
Note: Put your custom assembly as safe control in web.config.

And once we have our execution in code, we are the king.
We can do anything we want. like…
Fetch URL and check which Item from which list is clicked, redirect to its related page like sub site, related list, look up list, do some operation like creating sub site, creating multiple records anything according to your business requirement and then redirect back to same page or anywhere you want.
Here is a part snippet from my CustomRedirection.aspx

if (Request.QueryString["FROM"] == "ECB")
{
// find List id and Item id from URL
// use that data find item from list
// do manipulation according business logic

SPUtility.Redirect(strWebURL, SPRedirectFlags.Default, httpContxt);
}


Summary
1) Create an aspx page from feature and embed in the site.
2) Use page to place content.
3) Use page as middle way for seamless navigation.
Source page -> customeredirection page -> destination page.
4) Use page as a place where you can execute your code and comes back at your place again or you can go ahead.
5) Any many more way that may be I had not discovered yet.

We are using this feature mostly in custom navigation where we know source but don’t know the destination but we need to find destination with custom business logic and then redirect it.

XML CDATA and PCDATA Sections

Hi All,

I would like to bring a point on table about CDADA and PCDATA sections in XML Document.

PCDATA - Parsed Character Data


XML parsers normally parse all the text in an XML document.
When an XML element is parsed, the text between the XML tags is also parsed:
<message>This text is also parsed</message>
The parser does this because XML elements can contain other elements, as in this example, where the <name> element contains two other elements (first and last):
<name><first>Bill</first><last>Gates</last></name>

Parsed Character Data (PCDATA) is a term used about text data that will be parsed by the XML parser.

CDATA - (Unparsed) Character Data


The term CDATA is used about text data that should not be parsed by the XML parser.

Characters like "<" and "&" are illegal in XML elements.

"<" will generate an error because the parser interprets it as the start of a new element.

"&" will generate an error because the parser interprets it as the start of an character entity.

Some text, like JavaScript code, contains a lot of "<" or "&" characters. To avoid errors script code can be defined as CDATA.

Everything inside a CDATA section is ignored by the parser.

A CDATA section starts with "<![CDATA[" and ends with "]]>":

<script>
<![CDATA[
function GreaterThan(a)
{
if (a > 50) then
{
return 1;
}
else
{
return 0;
}
}
]]>
</script>

In the example above, everything inside the CDATA section is ignored by the parser.

Notes on CDATA sections:

A CDATA section cannot contain the string "]]>". Nested CDATA sections are not allowed.

The "]]>" that marks the end of the CDATA section cannot contain spaces or line breaks.

Lets have a simple scenario why we need this or where this can be useful.
Lets say you are developing a page or control for a list and you have some handy static logic that you want to fix in that.
Lets take an simple example saying that the value you pass in textbox can not greater than 50.
So what we can do is that use CDATA secion in XML with any tag. All you need to do is write you entire javascript in XML tag in CDATA secion. Fetch that node in Code using XMLDocument or any other XML class method.
and use Server side registration for client script. here your script will be coming from XML, so whenever you want to change the script, all you have to do is just change the XML and you are done with that.
I hope that sounds good and you got a good idea about how to use CDATA secion and use it for futire use.

Lets say you are developing a page or control for a list and you have some handy static logic that you want to fix in that.

Lets take an simple example saying that the value you pass in textbox can not greater than 50.

So what we can do is that use CDATA secion in XML with any tag. All you need to do is write you entire javascript in XML tag in CDATA secion. Fetch that node in Code using XMLDocument or any other XML class method.

and use Server side registration for client script. here your script will be coming from XML, so whenever you want to change the script, all you have to do is just change the XML and you are done with that.

I hope that sounds good and you got a good idea about how to use CDATA secion and use it for futire use.

Click here to refer more about XML



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