23 Mart 2012 Cuma

CRUD operations on SP list items

Small code piece to for CRUD operations of sharepoint list items


using (SPSite site = new SPSite("http://website url/" ))
// Or SPContext.Current.Site.Url
{
using (SPWeb Web = site.OpenWeb())
  {
            Web.AllowUnsafeUpdates = true;

            // Open List
             SPList list = Web.Lists["MyList"];

            // Add new item in List
             SPListItem item = list.Items.Add();
             item["Title"] = "Test Title";
             item["Description"] = "Test Description";
             item.Update();

            // Get Item ID
             listItemId = item.ID;

            // Update the List item by ID
             SPListItem item = list.GetItemById(listItemId);
             item["Description"] = "Update Description";
             item.Update();

            // Delete item
             SPListItem item = list.GetItemById(listItemId);
             item.Delete();

            Web.AllowUnsafeUpdates = false;
       }
 }

source:


22 Mart 2012 Perşembe

Sharepoint page trigger problem

Hi,
If you have a SP web page or a web part and the triggers (butoonClick, Update Panel etc) are fired only once and you need to refresh the page each time you d like to trigger the actions, you need to add the code below in your aspx page.




<script type="text/javascript">

_spOriginalFormAction = document.forms[0].action;
_spSuppressFormOnSubmitWrapper = true;
</script>


Source
http://stackoverflow.com/questions/6826002/code-to-download-file-from-sharepoint-2007-document-library-please-correct-me-i

Using DevExpress controls in Sharepoint page

Hi,
As you know DevExpress has great controls that you might find very handy to use in your Sharepoint web pages or web parts

To use a control what you need to do is:

- Register the control dlls  to GAC.
You can either use DevExpress Registerer tool for that, or you can manually copy the necessary dll s into assembly folder. I did the first.

- Register all the necessary dlls in aspx
You need to make sure that you have registered all the necessary control dll s in your aspx page.
If you just drag and drop the control into the page it does not register all the dll's. Just open the project References and add the line like below above your aspx page
<%@ Register
Assembly="DevExpress.Web.v8.2, Version=8.2.4.0, Culture=neutral, PublicKeyToken=9b171c9fd64da1d1"
Namespace="DevExpress.Web.ASPxRoundPanel" TagPrefix="dxrp" %>



You need to register the HttpHandler module.
You just need to add several lines into your portals web config.
Here is the link for that:
http://www.devexpress.com/Support/Center/p/K18262.aspx

- You need to add the necessary dll into SafeControl list.
In order to do that you need to add several lines into your portals web config.

Here is the link for that
http://www.devexpress.com/Support/Center/KB/p/K18109.aspx


Yeah, that's pretty much it, I think : )

Creating excel by code

Hi,

Sometimes you might need to create an export of a table as an excel file.
Of course there are thousands of tools for that but if you d like to do it manually, here is the code you need:

Sorry I have got the code from one of my collegues, I can not share the source link.


Response.Clear();
            Response.AddHeader("content-disposition", "attachment;filename=Export" + DateTime.Now.ToString("yyyy_MM_dd") + ".xls");
            Response.Charset = "";
            // If you want the option to open the Excel file without saving than
            // comment out the line below
            //Response.Cache.SetCacheability(HttpCacheability.NoCache);
            Response.ContentType = "application/vnd.xls";
            System.IO.StringWriter stringWrite = new System.IO.StringWriter();
            System.Web.UI.HtmlTextWriter htmlWrite =
            new HtmlTextWriter(stringWrite);
Table tbl = //Create your table in here !


tbl.RenderControl(htmlWrite);

//These both line are needed if you like to format the cells as text and number
            string style = @"<style>.text{mso-number-format:\@;text-align:left;};.Nums{mso-number-format:0\.00;};.unwrap{wrap:false}</style>";
            Response.Write(style);

            Response.Write(stringWrite);
            //Response.Write(stringWrite.ToString().Replace(".", ","));
            Response.End();


While you were creating the table you d like to render as excel, you can give the format as below:
row.Cells[0].Attributes.Add("class", "text");
row.Cells[1].Attributes.Add("class", "Nums");

"text" and "Nums" classes should be set after you have rendered your control. (Comments can be found on the first code fragment)

Excell number format styles

Hi,
Below is a useful info if you like to create an excel file by code and like to format the cells.

Source:
http://cosicimiento.blogspot.com/2008/11/styling-excel-cells-with-mso-number.html


Styling Excel cells with mso-number-format
mso-number-format:"0"NO Decimals
mso-number-format:"0\.000"3 Decimals
mso-number-format:"\#\,\#\#0\.000"Comma with 3 dec
mso-number-format:"mm\/dd\/yy"Date7
mso-number-format:"mmmm\ d\,\ yyyy"Date9
mso-number-format:"m\/d\/yy\ h\:mm\ AM\/PM"D -T AMPM
mso-number-format:"Short Date"01/03/1998
mso-number-format:"Medium Date"01-mar-98
mso-number-format:"d\-mmm\-yyyy"01-mar-1998
mso-number-format:"Short Time"5:16
mso-number-format:"Medium Time"5:16 am
mso-number-format:"Long Time"5:16:21:00
mso-number-format:"Percent"Percent - two decimals
mso-number-format:"0%"Percent - no decimals
mso-number-format:"0\.E+00"Scientific Notation
mso-number-format:"\@"Text
mso-number-format:"\#\ ???\/???"Fractions - up to 3 digits (312/943)
mso-number-format:"\0022£\0022\#\,\#\#0\.00"£12.76
mso-number-format:"\#\,\#\#0\.00_ \;\[Red\]\-\#\,\#\#0\.00\ "2 decimals, negative numbers in red and signed

19 Mart 2012 Pazartesi

CAML Query

CAML query does not filter any result

Hi,
If you are using the CAML for the first time and you have created your query with U2U (which you should)
you ll see that the query works in U2U browser but not in your code.

In order to fix that you should remove the <Query></Query> tags in your query : )

Yeah, it costs me 30 min. to figure it out.

Sharepoint changing max char property of multiline text field


Hi,
Since the default max char limit of any text field in sharepoint is set to 255, in order to increase it there are several ways.

One of them is writing a small csharp code as below:

using (SPSite mySite = new SPSite(siteName)) //site name
                {
                    SPWeb myWeb = mySite.OpenWeb();
                    SPList myList = myWeb.Lists[listName]; // Your document library Name
                    SPField myField = myList.Fields[fieldName]; //Your multiline field name
                    ((SPFieldMultiLineText)myField).UnlimitedLengthInDocumentLibrary = true;
                    myWeb.AllowUnsafeUpdates = true;
                    myField.Update();
                    myWeb.AllowUnsafeUpdates = false;
                }

In my case, I had a multiline field in infopath form and if you promote it with default settings it would have max char length 255.

Better way to achieve this problem, is creating a new custom field, with multiline text option and `Allow unlimited length .. ` property to Yes.

Then,
You can open Infopath and in the Form Options - Promoting settings, you can manually map your multiline field with your custom multiline field.

There you go, now you have a unlimited field !.


In my case, I have used this field to keep the repeating table data as json string, to read it in a custom sharepoint web page.


Third case is:

If you have created the field as singleline and already promoted but you ve realized that you needed as multiline to allow unlimited string, what you need to do is : (yeah, that was my case : ) )

1. Open the Infopath form, change the texbox field to Multiline.
2. Publish the form
3. Open the Sharepoint Manager, find the field and change the readonly property to false
4. Open the list in sharepoint, go to library settings, in the columns section, edit the propery and change it to multiline text by allowing unlimited length.
5. Yeay !

16 Mart 2012 Cuma

Infopath Security Validation Error WorkAround

Hi,
I guess with the Sharepoint 2010 SP 1 you might experience some security violation errors if you change the view of an infopath form. (web browser version)

To solve this issue there is a work-around.

You need to open 14 hive,
in TEMPLATE/LAYOUTS folder
below body section
<body runat=”server” id=”PageBody”>
add


<SharePoint:FormDigest runat="server" />
IIS reset might be needed.

Note:
Carefull with marks, in most sites it has ”  that ruins your site : )
This WA is from MS by the way.
Source:

9 Mart 2012 Cuma

Infopath Change a field in a one instance of repeating table

Hi,
If you like to change a field in one instance of the repeating table, please follow the link:

http://blogs.catapultsystems.com/sboldt/archive/2009/08/10/get-current-row-value-from-an-infopath-repeating-table.aspx

Basically you just need to use sender e.Site.CreateNavigator() method.
then use .MoveParent() method to access the current instance
Then use SelectSingleNode with "my:FIELDNAME" parameter.

8 Mart 2012 Perşembe

Infopath browser ignores OnChange Events

If you have Infopath forms which have onChanged events set on your controls. (i.e. TextBox, ComboBox), if you publish your forms with default settings you ll see the browser based forms ignore the OnChange events.

In order to fix that,
You should open the Properties of the each control bound with the event, go to Browser forms tab and Choose Always for Postback settings.