Logo 
Search:

Sharepoint Forum

Ask Question   UnAnswered
Home » Forum » Sharepoint       RSS Feeds

Use list from another site

  Asked By: Jamison    Date: Mar 29    Category: Sharepoint    Views: 1178

In the MiniCalendar WebPart I'm creating I'd like to allow the user to type in the url of an event list to use. Normally I only use lists from the current site I'm in by using the following code:

SPWeb objWeb = SPControl.GetContextWeb(Context);
SPList objList = objWeb.Lists[objGuid]

How can I change this code to get the list from a URL that the user provides?

Share: 

 

4 Answers Found

 
Answer #1    Answered By: Leif Cardenas     Answered On: Mar 29

I call the following Web Part code  my "Jump to an event  List" dropdown.
Written just for my good friend Monty.

It runs thru the current site  locating Event lists  and populating a
dropdown list  of options for the user  to choose from. If there are no
Event list, it renders some text instead. Let me know what you think...

//Begin C#
code-----------------------------------------------------------
private DropDownList dropdownlist = null;
protected override void CreateChildControls()
{
try
{
SPWeb web = SPControl.GetContextWeb(Context);

dropdownlist = new DropDownList();
SPListCollection lists = web.Lists;

ListItem listitem = new ListItem();
listitem = new ListItem();
listitem.Text = "Choose a list";
listitem.Value = String.Empty;
listitem.Selected = true;
dropdownlist.Items.Add(listitem);

foreach(SPList list in lists)
{
if(list.BaseTemplate ==
SPListTemplateType.Events)
{
listitem = new ListItem();
listitem.Text = list.Title;
listitem.Value = list.ID.ToString();
dropdownlist.Items.Add(listitem);
}
}

//Render the dropdown if there are event lists
if(dropdownlist.Items.Count > 1)
{
dropdownlist.AutoPostBack = true;
dropdownlist.SelectedIndexChanged += new
EventHandler(dropdownlist_SelectedIndexChanged);
this.Controls.Add(new LiteralControl("Choose an
Event list: "));
this.Controls.Add(dropdownlist);
}
else
{
this.Controls.Add(new LiteralControl("No Event
lists available on this site"));
}
}
catch{}
}

protected override void RenderWebPart(HtmlTextWriter output)
{
try
{
EnsureChildControls();
RenderChildren(output);
}
catch{}
}

private void dropdownlist_SelectedIndexChanged(object sender, EventArgs
e)
{
try
{
//Something other than "Choose a list" was choosen
if(dropdownlist.SelectedValue.Length > 0)
{
SPWeb web = SPControl.GetContextWeb(Context);
Guid guid = new
Guid(dropdownlist.SelectedValue);
SPList list = web.Lists.GetList(guid, false);
Page.Response.Redirect(list.DefaultViewUrl);
}
}
catch(Exception ex)
{
//Of course, you'll want better error handling
Page.Response.Write(ex.Message);
}
}
//End C# code-----------------------------------------------------------

If you really want the user to type  in the URL, I think we can make that
work too. Let me know.

 
Answer #2    Answered By: Jasper Hatfield     Answered On: Mar 29

This is almost what I was doing, but I didn't know about the
SPListTemplateType.Events.. I was just gathering all basetype lists  this helps
a lot.

However, the key piece that I'm missing is that I want to be able to point to an
events list  on the base portal site  (a global company events calendar) and then
be able to export the dwp and add it to any area or wss site and have it pull
from the same global events list rather than just a list on the current  site - I
need some way to get to the list on another site - I figured the easiest way
would be to type  in the url  for the site and then maybe show a dropdown of the
lists available. I've found that you can do this by using SPSite objSite = new
SPSite(http://www.mysite.com) to get the site in the url, but this should only
work for the top level site (sitecollection), and I'm not sure yet what happens
when you try to open a SPSite at the address of an SPWeb -- Is there a
difference I need to worry about? Is there a better way to do this?

The other minor change  I have in the code  is that the DropDownList is in a
custom toolpart.

I'll gladly share the code when I'm done, but would also like feedback on how
you could generalize the option that I'm adding for coloring days on the
calendar for events of a certain type.. Right now I have added an extra column
called "Type" which provides choices.. each choice shows with a different color
background (these choices can never have dates that overlap). This alows us to
see important information at a glance like holidays, timesheet due dates,
paydays, etc. I've hard coded a switch statement to provide the background
colors right into the web part, but I'd like to make it more dynamic than that..
Any suggestions? Anyone else interested in the final code? If so I'll just
post it here.

 
Answer #3    Answered By: Rashawn Hopper     Answered On: Mar 29

I guessing that you can easily modify my example to be a ToolPart rather
than rendering directly in the body of the WebPart. So, let me focus on
the crux of your two questions:

1. How do I open a specific SPWeb at a specific SPSite and iterate its
single Event list? and
2. How can I allow the end user  to map a color onto the Type field so it
isn't hardcoded?

For the first question, you already have half the code  to open a given
SPWeb:

//Get specific specified site  Collection
SPSite siteCollection = new SPSite("http://foo");

//Get child Web of Site Collection, if empty string the code
will return the root Web
SPWeb web =
siteCollection.OpenWeb("/absolute/urltothe/childsite");

Always remember to dispose of SPSite and SPWeb objects or your memory
footprint could overcome the server.

For the second question, I wonder if you couldn't create a ToolPart that
used a technique similar to the way that SharePoint lets us order
columns on a ListView. But instead of sequence numbers, you would use
unique colors. You would present a list  of Types based upon the unique
values in the Events List and to the right of each Type would be a drop
down list of colors. When the user choose a different color for a given
Type the rest of the drop down lists  would reshuffle so that they would
all be unique. You could potentially even reuse Microsoft's JavaScript
for reshuffling the drop downs (I've done that before). Then you would
store a delimited list of values as a custom property for the order of
colors the end user has chosen. If the custom property is empty you use
a canned color sequence, otherwise you use the color order chosen by the
end user. As long as you consistently retrieve the list of Types in the
same sequence you would never actually have to store the Type that was
related to a given color.

I drew up a quick HTML example, the Reorder JavaScript is ripped right
out of SharePoint's ViewEdit.aspx page. Paste the following HTML into a
text file, save it as an HTM and view it in a browser to see this idea
come to life:
<head>
<title>Color Chooser</title>
<script language="JavaScript">
function Reorder(eSelect, iCurrentField, numSelects)
{
var eForm = eSelect.form;
var iNewOrder =
eSelect.selectedIndex + 1;
var iPrevOrder;
var positions = new
Array(numSelects);
var ix;
for (ix = 0; ix < numSelects; ix++)
{
positions[ix] = 0;
}
for (ix = 0; ix < numSelects; ix++)
{

positions[eSelect.form["ViewOrder" + ix].selectedIndex] = 1;
}
for (ix = 0; ix < numSelects; ix++)
{
if (positions[ix] == 0)
{
iPrevOrder = ix + 1;
break;
}
}
if (iNewOrder != iPrevOrder)
{
var iInc = iNewOrder >
iPrevOrder? -1:1
var iMin = Math.min(iNewOrder,
iPrevOrder);
var iMax = Math.max(iNewOrder,
iPrevOrder);
for (var iField = 0; iField <
numSelects; iField++)
{
if (iField != iCurrentField)
{
if
(eSelect.form["ViewOrder" + iField].selectedIndex + 1 >= iMin &&

eSelect.form["ViewOrder" + iField].selectedIndex + 1<= iMax)
{

eSelect.form["ViewOrder" + iField].selectedIndex += iInc;
}
}
}
}
}
</script>
</head>
<body>
<form name="frmViewEdit" method="post">
<table cellspacing="0" cellpadding="0" border="0">
<tr>
<th align="left">Type</th>
<th align="left">Color</th>
</tr>
<tr>
<td width="100">Holidays</td>
<td>
<select onchange="Reorder(this,
0, 3)" name="ViewOrder0">
<option
value="1_Holidays" selected="selected">Red</option>
<option
value="2_Holidays">Green</option>
<option
value="3_Holidays">Blue</option>
</select>
</td>
</tr>
<tr>
<td>Timesheets</td>
<td>
<select onchange="Reorder(this,
1, 3)" name="ViewOrder1">
<option
value="1_Timesheets">Red</option>
<option
value="2_Timesheets" selected="selected">Green</option>
<option
value="3_Timesheets">Blue</option>
</select>
</td>
</tr>
<tr>
<td>Paydays</td>
<td>
<select onchange="Reorder(this,
2, 3)" name="ViewOrder2">
<option
value="1_Paydays">Red</option>
<option
value="2_Paydays">Green</option>
<option
value="3_Paydays" selected="selected">Blue</option>
</select>
</td>
</tr>
</table>
</form>
</body>

 
Answer #4    Answered By: Horace Coffey     Answered On: Mar 29

I'll implement the second example (probably through a custom toolpart builder) and let you know when it's done..

 
Didn't find what you were looking for? Find more on Use list from another site Or get search suggestion and latest updates.




Tagged: