zondag 15 april 2012

Check if Rich TextField is Empty


Last week I had some problems to do a check wether a Rich Text Box is filled in or not. 
When doing a check on the Text propertie of the TextField, 
I found out that Sharepoint puts default something in the TextField. 

Like it or not, but it's really frustrating that you can't use the JQuery test .isEmpty().
So after some testing, I found out that Sharepoint puts a Paragraph tag in the TextField. 
So here under you can found out how I did the check wether a Rich TextField is filled in or not. 
 

if( ($(ctl00_m_g_6e043a15_d2d5_4e91_a8c6_562e51fe3b46_ff121_ctl00_ctl00_TextField_inplacerte).text() == "") || ($(ctl00_m_g_6e043a15_d2d5_4e91_a8c6_562e51fe3b46_ff121_ctl00_ctl00_TextField_inplacerte).html() == "

?

") ) { alert('Please fill in the Rich Text box field !'); return false; }

maandag 9 april 2012

Update or Insert Sharepoint ListItems in a List Library


Looking for a way to update a Sharepoint ListItem by using the Lists.asmx webservice...
For doing this you need to create your function to create your Xml Layout
and afterwards call the function below UpdateSPList.
 

 XmlDocument xmlDoc = new XmlDocument();
 XmlElement elBatch = xmlDoc.CreateElement("Batch");
 elBatch.SetAttribute("OnError", "Continue");
 // this because if one of the methods gives an error it will continue with the next method
 elBatch.SetAttribute("ViewName", Sharepoint_ViewId);
 elBatch.InnerXml = strBatch;
 
 returncode = UpdateSPList( sSiteUrl,  sSiteList,  elBatch);


 
strBatch containing for example:
        
<Method ID='0' Cmd='Update'>
    <Field Name='ID'> SharepointListItem.ows_ID </Field>
</Method>
<Method ID='1' Cmd='Update'>
    <Field Name='ID'>OtherSharepointListItem.ows_ID </Field>
</Method>
<Method ID='0' Cmd='New'>
            <Field Name='ID'>New</Field>
            <Field Name='Title'>YOUR TEXT HERE</Field>
</Method>
<Method ID='1' Cmd='New'>
            <Field Name='ID'>New</Field>
            <Field Name='Title'>YOUR TEXT HERE</Field>

 



Remark: The next Chars must be translated in the text values for your Fieldtag:
    "&" ==> "&"
    "<" ==> "<"
    ">" ==> ">"
    "\"" ==> """
    "'" ==> "'"

Below you find my Function that I have created that actualy does 
the update or insert into your List.
It requires three parameters : 
- the Site Url you are working on
- the List name on which you want to add or update one or more items
- the XML that contains the Batch Element

You will see that the function also uses the next global parameters
- sTechnicalUser
- sTechnicalPw
- sDomain
- bDefaultLogon (if true you will connect to the webservice with the 
user that is executing the program, otherwise use the technical user with it's
password and domain.  
This is done for giving the possibility to let shedule the program. 
         
 private Boolean UpdateSPList(string sSiteUrl, string sSiteList, XmlElement elBatch)
        {
            Boolean retcode = true;

            REFERANCENAME.Lists list = new REFERANCENAME.Lists();
            list.Url = sSiteUrl + "/_vti_bin/lists.asmx";
            if (bDefaultLogon)
                list.Credentials = System.Net.CredentialCache.DefaultCredentials;
            else
            {
                list.PreAuthenticate = true;
                list.Credentials = new System.Net.NetworkCredential(sTechnicalUser, sTechnicalPw, sDomain);
            }
            logThis("Start updateSPList " + sSiteUrl + " " + sSiteList + " ...");
            try
            {
                XmlNode ndReturn = list.UpdateListItems(sSiteList, elBatch);
                logThis(sSpaces + "return " + ndReturn.OuterXml);
                // Instantiate a new XML document object to hold the return value(s)
                XmlDocument xmlResult = new XmlDocument();
                xmlResult.LoadXml(ndReturn.OuterXml);
                // SharePoint XML always uses a fixed namespace; you'll need your own NamespaceManager object to parse the return values
                XmlNamespaceManager nsMgr = new XmlNamespaceManager(xmlResult.NameTable);
                nsMgr.AddNamespace("sp", ndReturn.NamespaceURI);
                XmlNode ndRoot = xmlResult.SelectSingleNode("sp:Results", nsMgr);
                // Find the ErrorCode node, which exists for all operations regardless of status.
                XmlNodeList nlResults = ndRoot.SelectNodes("//sp:Result/sp:ErrorCode", nsMgr);
                // Loop through the node collection and find each ErrorCode entry
                foreach (XmlNode ndResult in nlResults)
                {
                    // Check the value of the node to determine its status
                    if (ndResult.InnerText != "0x00000000")
                    {
                        XmlNode ndError = ndResult.NextSibling;
                        string sError = ndError.InnerText;
                        // Set the value of string variable to hold the error code
                        this.logThis("Update operation failed for " + sSiteList + ": " + sError + ".");
                        // If you want to trip the Try…Catch, throw and exception of whatever type suits you
                        iFailedUpdatedRecords++;
                    }
                    else
                    {
                        iUpdatedRecords++;
                    }
                }
           }
            catch (Exception e)
            {
                logThis(sSpaces + "Error UpdateSPList ", e);
                retcode = false;
            }
            finally
            {
                list.Dispose(); // important!!!! always dispose your sharepoint objects !!!!!
                logThis("End updateSPList " + sSiteUrl + " " + sSiteList);
            }
            return retcode;
        }
 


donderdag 5 april 2012

Block Excape and Ctrl+z keypress to prevent the undo action by keyboard


One of the disadvantages of using Jscript of JQuery is when a user uses 
the Escape or CTRL+z on his keyboard that 
your actions, like filling in input fields, in your scripting are being undone.

So best option in this case is to prevent that thoose events are being executed.
This you can do by capturing the keypresses of the Escape key and the CTRL+z key combination.
The script below will execute on the document Keydown event. 

 function DisableEscAndCtrlZ(){
  document.onkeydown = function(evt) {
   evt = evt || window.event;
   // ctrl+z
   if (evt.ctrlKey && evt.keyCode == 90) {
    return false;  
   }
   // esc
   if (evt.keyCode == 27) {
    return false;  
   }
  };
 }

 $(document).ready( function() {
  DisableEscAndCtrlZ();
 };
 

maandag 2 april 2012

View SharePoint Attachments in Display Pages


When creating a Custom Page of the Type Display, Sharepoint does not include
the attachments in your page.

To display attachments that dissapear in a custom display form,
simply add a new row in the custom display form 
and add the following line within that row in the code view.


<SharePoint:AttachmentsField ControlMode="Display" FieldName="Attachments" runat="server" Visible="true"/>
 

Enyoi your attachments in your Display ASPX pages ;-)