Showing posts with label Code Snippet. Show all posts
Showing posts with label Code Snippet. Show all posts

SharePoint - Picture Library - How to copy picture from one Picture Library to another

Hi All,

Here it is interesting blog post of an eventhandler.Below is the requirement for the task.

There are two picture libraries available, i need to copy picture from one library to another when picture is going to upload in picture library.For that i've created "ItemAdded" event handler and do code on this event.

You can find the code below:

public override void ItemAdded(SPItemEventProperties properties)
{
base.ItemAdded(properties);
try
{
SPListItem item = properties.ListItem;
byte[] picFile = null;
SPFolder Sourcelibrary = item.Web.Folders[name of the source picture library];
picFile = item.File.OpenBinary();
if (picFile != null && picFile.Length > 0)
{
SPFolder DestLibrary = item.Web.Folders[name of the destination picture library];
item.Web.AllowUnsafeUpdates = true;
DestLibrary.Files.Add(System.IO.Path.GetFileName(item.File.Name), picFile);
}
}
catch (Exception)
{
throw;
}
}

Adding users to SharePoint Programmatically

Hi All

The code snippet shows you how to add users to SharePoint Programmatically. This code looks for a User who registers with FBA site and Pull them in to SharePoint Group.

[C# code snippet]
MembershipCreateStatus status;
If (Membership.GetUser("UserName") == null)
{
//Insert into MemberShip's Database
Membership.CreateUser("UserName", "Password", "Email", "Not used", "Not used", true, out status);
If (status.ToString().ToLower().Equals("success"))
{
MembershipUser User=Membership.GetUser ("UserName");
Using (SPSite site = new SPSite ("http://sharepoint site url"))
{
Using (SPWeb web = site.OpenWeb())
{
string _usernameWithProvider = String.Format("{0}:{1}", System.Web.Security.Membership.Provider.Name, User.UserName);
web.AllowUnsafeUpdates = true;
SPGroup grp = GetGroup ("Visitor", web); //Visitor is group available with you SharePoint site.
if (grp!= null)
{
web.SiteUsers.Add(_usernameWithProvider, User.Email, User.UserName, "");
web.Groups [group].AddUser(_usernameWithProvider, User.Email, User.UserName, "");

}
}
}
} }

private SPGroup GetGroup(string groupName, SPWeb web)
{
SPGroup spReturn = null;
try
{
spReturn = web.Groups[groupName];
}
catch (Exception ex)
{
return spReturn;
}
return spReturn; }

Hopefully This helps...