Tuesday, September 3, 2013

How to get Site, Web, and List name from a list url

Some time we have url of list and need to find its web, list name ...

Basically what we do simply split string and get web name and list.
This approach will not give us dynamic solution.
We have SharPoint to do this in easy way and also dynamic... :)

 Using siteCollection As New SPSite(listurlfromitems)
            Dim Site As SPSite = siteCollection
            Dim myWeb As SPWeb = siteCollection.OpenWeb()
            Dim _list As SPList = myWeb.GetList(listurlfromitems)
End Using

Here listurlfromitems is a string which holds URL of list
after that we can find site, web, and list in Site, myWeb, _list variable...

Get Data of current login user in Sharpoint

some time we need to get data according to current user.
Like items that has been created by current login user.

And there so many approach to do this but what i will recommend that never goes wrong.

  osb.Append("     <Where>")
  osb.Append("                       <Eq>")
   osb.Append("                            <FieldRef Name=""Author"" LookupId=""True"" />")
    osb.Append("                            <Value Type=""Lookup""   >" & SPContext.Current.Web.CurrentUser.ID & "</Value>")
    osb.Append("                        </Eq>")
    osb.Append("         </Where>")


Just add this query and get what you wanted :D

Friday, August 16, 2013

How to read list items using REST in Sharepoint 2010


Here I am giving on simple example that will read data from a list using REST.
Earlier we need to use SPAPI for this task but we can do very easily with REST.

var queryUrl ="http://YourSitename/_vti_bin/ListData.svc/ListName";

$.ajax({
    url: queryUrl,
    accepts: {
        json: "application/json;odata=verbose"
    },
 dataType: "json",              //By Default it returns in xml formate
    method: "GET",
    success: onQuerySuccess,
    error: onQueryError
});

function onQuerySuccess(data) {
var i =parseInt(0);
    if (data) {
        $.each(data.d.results, function () {
            alert(data.d.results[i].Title);    // give your internal column name of list
i=parseInt(i)+parseInt(1);
        });
    }
}
function onQueryError(error) {
   alert("ee");
}


We can get any column’s value.
data.d.results[i].Title

Here “Title” is the name of column so you can change the column name and get your data.

Thanks



Wednesday, August 7, 2013

How to get attached file url in sharepoint

Hi Friends,
I was searching one solution that how can i have url of Attached item, but mostly i got hard coding
:(

mostly searched solution is like this
"/you Stie/List/Your list name/Attachments/" + item("ID").ToString() + "/" + obj.ToString
and this is really bad to use.
But now i have explored Sharepoint and get solution, and its very simpl.

Here is the solution.
 dim _url as string= item.Attachments.UrlPrefix.ToString + item.Attachments.Item(0).ToString

Explations:
1)
item.item.Attachments.UrlPrefix
it will return till id value in the form of url
2)
 item.Attachments.Item(0).ToString

This will return the item name that we need
by this solution we will have our url of Attached item


I hope this will helpful for you, If u are facing any problem regarding of this plz let me know through ur post

How to detect escape key press with javascript


Hi Frnds ...

I was using some code for escape key handle but it was working fine only in IE,  Not in Chrome. So i have stared doing some R&D and finally got solution :-)
And really it is working fine in all

<script type="text/javascript">
 document.onkeydown = function(evt) {
    evt = evt || window.event;
    if (evt.keyCode == 27) {
        alert("Escape");
    }
};
</scrip>

Dynamically Create CAML QUERY

 By using this code we can create a CAML Query at run time .
\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
 Public Function topic_Wise_Collection(ByVal strin As ListItemCollection) As String
        Dim str As String = "<Where>"
        Dim firstLine As String = ""
        Dim starting As String = ""
        Dim midll As String = ""
        Dim i As Integer = 0
        For Each _item As ListItem In strin
            Dim _topic01 As String = _item.ToString
            If i = 0 Then
                firstLine += "<Or>"
                firstLine += "<Contains>"
                firstLine += "<FieldRef Name=" + "Topic0" + " />"
                firstLine += "<Value Type=" + "Text" + ">" + _topic01 + "</Value>"
                firstLine += "</Contains><Contains>"
                firstLine += "<FieldRef Name=" + "Topic0" + " />"
                firstLine += "<Value Type=" + "Text" + ">" + _topic01 + "</Value>"
                firstLine += "</Contains>"
                firstLine += "</Or>"
            Else
                Dim befor As String
                befor = "<Or><Contains><FieldRef Name=" + "Topic0" + " />"
                befor += "<Value Type=" + "Text" + ">" + _topic01 + "</Value>"
                befor += "</Contains>"
                midll = befor + midll
                firstLine += "</Or>"
            End If
            i = i + 1
        Next
        str += midll + firstLine + "</Where>"
        Return str
    End Function

Hot to get Current user name using SPAPI

 This is so simple just need to follow step in correct way
<-----Start------------------->
<script src="/SPAPI_Core.js"></script>
<script src="/SPAPI_Lists.js"></script>
<script src="/jquery-1.4.1.js"></script>
<script type="text/javascript">
$(function (){
 var nameOnly = getCurrentUserName();
 alert(nameOnly);
});
function getCurrentUserName(){
// put path of user information list
var lists = new SPAPI_Lists('path for User information list is');
// put the id of user information list
var items = lists.getListItems('888216C4-B998-4889-91C2-16D68127010A','','<Query><Where><Eq><FieldRef Name="ID"/><Value Type="Counter">'+ _spUserId +'</Value></Eq></Where></Query>','','');
 if (items.status == 200)
 {
 var rows = items.responseXML.getElementsByTagName('z:row');
  for (var i=0; i<rows.length; i++)
  {
    return rows[0].getAttribute('ows_Title');
   break;
  }
 }
 else
 {
  alert("Error in getting user");
 }
}
</script>
<-----End------------------->

If you are getting any error then check ur linked file path.

Thanks