Tuesday, August 27, 2013

How to get current logged in user’s group name in SharePoint

Based on my project requirement i need the current logged in user's group name so that we can hide and show something on page load.

Method 1

Dim _site As New SPSite(SPContext.Current.Site.Url)
Dim web As SPWeb = _site.OpenWeb()
For Each gr As SPGroup In web.Groups
If SPContext.Current.Web.IsCurrentUserMemberOfGroup(gr.ID) Then
If (gr.Name.ToString.Trim.ToLower = "Owners".ToString.Trim.ToLower) Then
actionbutton.Visible = True ' hide here 
End If
End If
Next


The above method will get all group's from Current site ..then checking  user is member of any group  if it match  checking for particular group name   to hide somthing in page


Method 2

For Each group As SPGroup In SPContext.Current.Web.CurrentUser.Groups
Dim groupName As String = group.Name
Response.Write(groupName)
Next

 The above method will get all group's which belongs to Current Login User Directly

Monday, August 26, 2013

How To Get List Item and attached file from particular list using Sharepoint API

Just Add 

<script src="/Style Library/JS/jquery-1.5.1.js" type="text/javascript" ></script>
<script src="/Style Library/JS/jquery-1.6.2.min.js" type="text/javascript" ></script>

<script type="text/javascript">

$(function () {
var lists = new SPAPI_Lists('http://yoursite/msasiaservices/');
var items = lists.getListItems('Announcements');
if (items.status == 200)  {
var rows = items.responseXML.getElementsByTagName('z:row'); // Get only list rows
for (var i = 1; i < rows.Count; i++) {
var title = rows[i].getAttribute('ows_Title');
var body = rows[i].getAttribute('ows_Body');
document.getElementById('heading1').innerHTML = title
document.getElementById('lbl1').innerHTML = body
var _id = rows[i].getAttribute('ows_ID');
var attachid = lists.getAttachmentCollection("Announcements", _id);
if (attachid.status == 200) {
var attachid = attachid.responseXML.getElementsByTagName('Attachment');
if (attachid.length > 0) {
for (var i = 0; i < attachid.length; i++) {
alert($(attachid[i]).text())
document.getElementById("img1").src = $(attachid[i]).text()
}}} }    
});

</script>

Sunday, August 25, 2013

How to Add or Delete List Items in Sharepoint (Inside Folder)

Creating List Item

Dim mySite As SPWeb = SPContext.Current.Web

Dim listItems As SPListItemCollection   = mySite.Lists(TextBox1.Text).Items
Dim item As SPListItem = listItems.Add()
item("Title") = TextBox2.Text
item("Stock") = Convert.ToInt32(TextBox3.Text)
item("Return Date") = Convert.ToDateTime(TextBox4.Text)
item("Employee") = TextBox5.Text
item.Update()

Deleting List Item

Dim mySite As SPWeb = SPContext.Current.Web

Dim listItems As SPListItemCollection = mySite.Lists(TextBox1.Text).Items
Dim itemCount As Integer = listItems.Count
Dim k As Integer
For k = 0 To itemCount - 1
    Dim item As SPListItem = listItems(k)
    If TextBox2.Text = item("Employee").ToString() Then
        listItems.Delete(k)
    End If
Next k

Deleting List Item By ID


Dim mySite As SPWeb = SPContext.Current.Web

Dim listItems As SPListItemCollection = mySite.Lists(TextBox1.Text).Items
listItems .Items.DeleteItemById(deleteItemId) ' pass id here

Updating List Item


Dim siteUrl As String = "http://MyServer/sites/MySiteCollection"

Dim clientContext As New ClientContext(siteUrl)
Dim oList As List = clientContext.Web.Lists.GetByTitle("Announcements")
Dim oListItem As ListItem = oList.Items.GetById(3)
oListItem("Title") = "My Updated Title."
oListItem.Update()
clientContext.ExecuteQuery()


Creating List Item Inside Folder In a Sharepoint List


Dim a As SPSite = New SPSite(AppSettings("SPUrl"))

Dim websp As SPWeb = a.OpenWeb()   
Using Web As SPWeb = root_web
Dim list As SPList = Web.Lists("ListName")
Web.AllowUnsafeUpdates = True
If list IsNot Nothing Then
Dim _folderitem As SpFolder = List.RootFolder.SubFolder("FolderName")
Dim _item As SPListItem = list.Items.Add(_folderitem.Folder.ServerRelativeUrl, SPFileSystemObjectType.File)
_item("Booking_x0020_Number") = Me.lblBookingNo.Text
_item("Duration") = timeSlots
_item("Booking_x0020_Date") = Date.Now                           
_item.Update()                     
End If
End Using

Retrieving List Item From Folder In  a Sharepoint List


Dim a As SPSite = New SPSite(AppSettings("SPUrl"))

Dim websp As SPWeb = a.OpenWeb()
Using Web As SPWeb = root_web
Dim list As SPList = Web.Lists("ListName")
If list IsNot Nothing Then
Dim _folderitem As SpFolder = List.RootFolder.SubFolder("FolderName")
Dim Query As Spquery = New Spquery()
Query .Folder = _folderitem
Dim Items As SPListItemCollection = _list.GetItems(query)
For Each _ids As SPListItem In Items 
Me.lblBookingNo.Text    =  _item("Booking_x0020_Number") 
Next  
End If
End Using






A list of Sharepoint Virtual File paths

Path and file explanation
_layouts/viewlists.aspx Same as site actions->View all content
_catalogs/masterpage/Forms/AllItems.aspx View master pages for this site collection.
_layouts/changesitemasterpage.aspx Changes the master page for the site collection (must be called from the site collection url, not a subweb url)
_layouts/permsetup.aspx assings the 3 magic groups to a sharepoint web called from a web or subweb url
_catalogs/lt/Forms/AllItems.aspx List template Gallery
_catalogs/solutions/Forms/AllItems.aspx Site Template (/Solutions) Gallery
_layouts/settings.aspx Site Settings
_layouts/user.aspx A list of all groups and users in a given Site
_layouts/groups.aspx A list of all groups in a given Site Collection
_layouts/AreaTemplateSettings.aspx This screen chooses what site templates are available when creating a new site in a given site collection
_layouts/quiklnch.aspx An almost odd list view of the quick launch items –
but not the one you get to from site settings.
This is linked to from the “getting started” web part.
_layouts/qlreord.aspx Same as above – this one lets you sort the quick launch items.
_layouts/AdminRecycleBin.aspx End User Recycle Bin.
_layouts/AdminRecycleBin.aspx?View=2 Deleted from End User Recycle Bin.
Pagename.aspx?contents=1 View the web parts on a page – good for times when a web part keeps a page from rendering in normal mode.

How to get a list of all users in SharePoint


All users that exist in a SharePoint web (but don't necessarily have permission):
Users that exist in a SharePoint web but have been granted some permission:
Users that exist in a SharePoint site collection (but don't necessarily have permission):
Note: I am assuming you are using WSS 3.0 / MOSS 2007 but this should work for WSS 2.0.
Also look at:

eg:-
Dim Site As New SPSite("SiteURL")
Dim AllUsers As SPUserCollection = Site.RootWeb.AllUsers
Dim u As SPUser
For Each u In AllUsers
    Response.Write(u.LoginName & " " & u.Name & " " & u.Email & "<br />")
Next


Diifrent ways to get logged in User Name & Handling Changes in Form Based Authentication User's


  • To get the logged in user name you have the following methods available in SharePoint 2010 Web Part User Control:

  

  • this.Page.User.Identity.Name
  • HttpContext.Current.User.Identity.Name
  • SPContext.Current.Web.CurrentUser.LoginName
  • SPContext.Current.Web.CurrentUser.Name



   Programmatically converting login name to claim and vice versa



string userName = null;
SPClaimProviderManager mgr = SPClaimProviderManager.Local;
if (mgr != null)
{
    userName = mgr.DecodeClaim(SPContext.Current.Web.CurrentUser.LoginName).Value;
}

Claims back and forth


string userName = null;
SPClaimProviderManager mgr = SPClaimProviderManager.Local;
if (mgr != null)
{
    SPClaim claim = new SPClaim(SPClaimTypes.UserLogonName, "myuser", "http://www.w3.org/2001/XMLSchema#string", SPOriginalIssuers.Format(SPOriginalIssuerType.Forms, "myprovider"));
    userName = mgr.EncodeClaim(claim);
}

Wednesday, July 24, 2013

Removing HTML Tags and Java Scripts Tags From String Using Regular Expresion

From today, I am trying to post again on a regular basis

Here is a one line regular expression to remove a html tag from a string.....

In the example given below I am stripping the Script tags from the given string.


      <!--[if IE]>
    <script>
        if (!document.documentMode || (document.documentMode < 9))
        {
            document.write("<script src=\"http://content.jobsdb.com.sg/Content/v184/Javascript/html5.js\"><\/script>");
            document.write("<script src=\"http://content.jobsdb.com.sg/Content/v184/Javascript/html5p.js\"><\/script>");            
            document.write("<style>.job-ad-applytool {text-align: center;}</style>");
        }
    </script>

    <![endif]-->


 First try to remove the Script Tags From String using Below regular expression..

<script>[\d\D]*?>[\d\D]*?</script>     --> this  expression removes script tags from string

eg:-

 Dim _content As String = Regex.Replace(_pageBody.TrimStart(), "<script>[\d\D]*?>[\d\D]*?</script>", "", RegexOptions.Multiline)

later u remove all Html Tags   From string  using Below Regular Expression

<.*?>                       ---->  this expressions remove all html tags..


eg:-

 Dim _content As String  = Regex.Replace(_pageBody.TrimStart(), "<.*?>", "", RegexOptions.Multiline)



it' ll Remove all script tag's and Html Tags From string ...:)